为何加了代码 B.prototype = new A('test') 后,
B.prototype.constructor变为function A(){}
function A(){}
console.log(A.prototype.constructor);//function A(){}
function B(){}
console.log(B.prototype.constructor);//function B(){}
B.prototype = new A('test');
console.log(A.prototype.constructor);//function A(){}
console.log(B.prototype.constructor);//function A(){}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
其实B.prototype上并没有constructor属性,而是随着原型链,找到A.prototype.constructor
其实在此处一定要理解好
B.prototype本身就是一个对象(new A('test'))。B.prototype上找不到constructor属性,自然就要顺着这个对象的原型链查找这个属性了。
谢邀。
这句重置了原型链的指向,此时
B.prototype是A的实例,然后A的实例的constructor的指向当然是其原型上的constructor自身了