function parent(x) {
this.x = x;
}
parent.prototype.name = 'dili';
parent.prototype.age =40;
function child(x) {
parent.call(this, x); //继承属性
}
child.prototype.age = 23
// child原型对象指向parent的实例化对象
child.prototype = Object.create(parent.prototype);
// child原型构造函数指向child
child.prototype.constructor = child;
const fn = new child('x');
console.log(fn.name, fn.age) // 'dili',23
使用以上方法可以实现多层函数的继承,但并不影响构造函数的指向。
fn.__proto__ === child.prototype;
//child.__proto__ === Function.prototype
child.prototype.__proto__ === parent.prototype
//parent.__proto__ === Function.prototype
parent.prototype.__proto__ == Object.prototype
//Function.__proto__ === Function.prototype
//Function.prototype.__protope === Object.prototype 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1978/viewspace-2804314/ ,如需转载,请注明出处,否则将追究法律责任。