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
例子:
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);
