原型链介绍
- 每一个对象都有自己的原型链,而原型链也是对象,也有自己的原型,以此类推形成链式结构,称之为原型链
对象访问原型链规则:就近原则
- 对象优先访问自己的属性,如果自己没有属性才会访问原型对象的属性 ,以此类推知道原型链的终点(null)如果还没有属性则返回undefined 方法会报错xxx is not function
instanceof运算符
- 检测某个构造函数prototype在不在实例对象原型链上
- 语法;实例对象 instanceof 构造函数
let arr = [10, 20, 30]
let str = new String('abc')
// arr->Array.prototype->Object.prototype->null
console.log(arr instanceof Array) //true
console.log(arr instanceof Object) //true
console.log(arr instanceof String) //false
// str->String.prototype->Object.prototype->null
console.log(str instanceof Array) //false
console.log(str instanceof Object) //true
console.log(str instanceof String) //true
面试点常问
- instanceof运算符原理,检测右边构造函数prototype是否在左边实例对象的原型链中
2.原型链继承:把想要拥有成员的目标对象的空实例对象赋值给自己的原型对象,就可以完成继承目标对象的所有成员
我们总得做一次让自己值得骄傲的事吧