Uncaught TypeError is not a constructor?

首先

function a(){
    this.a1="haha";
}
b=new a();
console.log(b);

可以正常运行的毋庸置疑。

那么改一下

a=function (){
    this.a1="haha";
}
b=new a();
console.log(b);

同样可以正常运行。

再改一下

a=function (){
    this.a1=function (){
        this.a2="haha";
    };
    e=new this.a1();
    console.log(e);
}
b=new a();

还是可以正常运行。

但是再改一下就不行了

a=function (){
    this.a1=function (){
        this.a2="haha";
    };
}
b=new a();
e=new a.a1();

chrome控制台提示Uncaught TypeError: a.a1 is not a constructor

想问一下这是什么原因?

阅读 9.6k
2 个回答

a里面没有a1
clipboard.png

因为a方法没实例化,所以你找不到构造的私有属性a1。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题