原始的原型链是怎样玩的

带着问题看代码:

1、原始的继承是怎样实现继承的? A类的prototype 属性 = B类的实例

2、实现继承后,连B类的中实例的属性(放在了A类的prototype中)和原型链的上的东西都可以用

3、A.prototype.constructor实际上已经指向了B--被重写了(但是不影响对实际代码运行的理解)

4、原型链继承,是往上找,找到了直接就用了,就不再往上找了

javascript 复制代码
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
相关推荐
坐吃山猪2 小时前
SpringBoot01-配置文件
java·开发语言
晚风(●•σ )2 小时前
C++语言程序设计——06 字符串
开发语言·c++
我叫汪枫2 小时前
《Java餐厅的待客之道:BIO, NIO, AIO三种服务模式的进化》
java·开发语言·nio
Nicole-----3 小时前
Python - Union联合类型注解
开发语言·python
晚云与城3 小时前
今日分享:C++ -- list 容器
开发语言·c++
兰雪簪轩3 小时前
分布式通信平台测试报告
开发语言·网络·c++·网络协议·测试报告
FPGAI4 小时前
Qt编程之信号与槽
开发语言·qt
Swift社区4 小时前
从 JDK 1.8 切换到 JDK 21 时遇到 NoProviderFoundException 该如何解决?
java·开发语言
0wioiw05 小时前
Go基础(④指针)
开发语言·后端·golang
gnip5 小时前
Jst执行上下文栈和变量对象
前端·javascript