原型链叫隐式原型链,是由__proto__
属性串联起来,原型链的尽头是Object.prototype
当访问一个对象的某个属性时,会先在这个对象本身属性上查找,如果没有找到,则会去它的proto隐式原型上查找,即它的构造函数的prototype,如果还没有找到就会再在构造函数的prototype的proto中查找,这样一层一层向上查找就会形成一个链式结构,我们称为原型链。
看原型链主要是用__proto去找内容
child实例化对象去找属性
{
//原型链
//child实例化对象找属性
//构造函数
function Person(mon){
this.mother=mon;
Person.prototype.father='xy'
}
Object.prototype.father='xh'
let child= new Person('ABC')
console.log(child);
//输出实例化对象身上的属性
console.log(child.mother);
// console.log(child.father);
//若在对象身上没找到father属性,则会去它的__proto__隐式原型上找
//指向Person
console.log(child.__proto__.father);
//指向Object
console.log(Person.prototype.__proto__.father);
//指向最外层未定义属性
console.log(Object.prototype.__proto__);//null
}
预览: