当前位置: 首页> 健康> 科研 > 原始的原型链是怎样玩的

原始的原型链是怎样玩的

时间:2025/8/22 22:58:21来源:https://blog.csdn.net/weixin_43131046/article/details/140884762 浏览次数:0次

带着问题看代码:
1、原始的继承是怎样实现继承的? A类的prototype 属性 = B类的实例
2、实现继承后,连B类的中实例的属性(放在了A类的prototype中)和原型链的上的东西都可以用
3、A.prototype.constructor实际上已经指向了B–被重写了(但是不影响对实际代码运行的理解)
4、原型链继承,是往上找,找到了直接就用了,就不再往上找了

function subType (j) {this.name = 'subType'this.nameJ = j
}
subType.prototype.getValue = function () {return  'subType原型上的值'
}function deviceType (k) {this.nameOther = 'deviceTye'this.nameK = k
}
// 这种方法实现的继承,就是连constructor中的属性就也给继承了
deviceType.prototype = new subType()
deviceType.prototype.getValueOther= function() {return 'deviceType原型链上的值'
}let instance = new deviceType(99)// 继承的表现,可以看到自己原型上的,和继承某个实例对象原型链上的东西
console.log(instance.nameOther) // deviceTye
console.log(instance.name) // subType
console.log(instance.getValueOther()) // deviceType原型链上的值
console.log(instance.getValue())  // subType原型上的值// 这行打印可以看到是怎样的,(继承某个实例的属性)会放在deviceType.prototype.
console.log(instance.__proto__) // { name: 'subType', getValueOther: [Function (anonymous)] }// 会发现被重写了
console.log(instance.constructor) //  [Function: subType]
// 打印一下完整的原型链
console.log(instance.__proto__.__proto__.constructor) // [Function: subType]// 虽然被重写了,但是不影响实例化
console.log(instance.nameK)  // 99
console.log(instance.nameJ) // undefined
关键字:原始的原型链是怎样玩的

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: