原型与原型链
原型:prototype => 函数特有的,数组对象不具备
原型链:_ proto _ / [[prototype]] (浏览器体现形式) => 任何数据类型都有原型链
function fn (){}fn.prototype.name = "zhangsan"fn.prototype.fn2 = function(){log("111")}
作用:为了继承
function Person(){}Person.prototype.name = "zhangsan"Person.prototype.age = 18Person.prototype.getAge = function(){ console.log(this.age)}// new 一个实例let person1 = new Person()console.log(person1.name) // zhangsanperson1.getAge() // 18person1.gender // not defined
原型链的查找规定:就近准则
从以后实例去找,找到就返回,如果没找到,则顺着原型链一层一层往下来找,直到找到null为止(原型链顶端)还没找到,报错
通过hasOwnProperty()判断是公有属性还是下层的
let item;for(item in person1){ if(person1.hasOwnProperty(item)){ console.log(item) }}
hasOwnProperty()办法是挂载在Object办法