关于构造函数是否一定要返回this
javascript吧
全部回复
仅看楼主
level 12
microroom 楼主
当函数作为构造函数使用时,默认是返回当前创建的对象的引用(即this),其实可以返回其它引用。
例子:
let p;
function Person(nm,a)
{
this.name=nm;
this.age=a;
p=this;
return new Student('AAA',16,99);
}
function Student(nm,a,score)
{
this.name=nm;
this.age=a;
this.score=score;
}
let s=new Person('zhs',28);
console.log(p);
console.log(s);
2022年03月10日 02点03分 1
1