原型与原型链

2020-12-10  本文已影响0人  肾仔博

一、prototype
在JavaScipt中,每个函数都有一个prototype属性,这个属性指向着函数的原型对象。
二、__ proto __
这是每个对象(除null外)都会有的属性,叫做__ proto __,这个属性指向该对象的原型。
三、constructor
每个原型对象都有一个constructor属性,指向该关联的构造函数。

//动物
function Animal () {
  this.eat = function(){
        console.log("animal eat");
  }
}
//狗
function Dog(){
  this.bark = function(){
       console.log("dog bark");  
  }
}
Dog.prototype = new Animal();
//哈士奇
var hashiqi = new Dog();
console.log(hashiqi.__proto__ === Dog.prototype);  //显示:true
console.log(Dog.prototype.__proto__ === Animal.prototype);  //显示:true
console.log(hashiqi.bark());  //显示:dog bark
console.log(hashiqi.__proto__.eat());  // 显示:animal eat
原型链.png

参考:https://www.cnblogs.com/loveyaxin/p/11151586.html

上一篇 下一篇

猜你喜欢

热点阅读