浅谈原型链

js原型链

JS 原型链有两个比较重要的属性:__proto__prototype

  • __proto__ 我的理解就是一个指针,该指针指向其构造函数的原型对象prototype
  • prototype 是构造函数的一个属性,你可以理解为一个空对象,可以在该对象内写任意的方法和属性,由该构造函数实例化的对象的__proto__指针会指向 该构造函数的 prototype

构造函数也是对象,也有 __proto__,也会指向其构造函数的原型对象 prototype......一环扣着一环这就是 JS 的原型链和继承原理

但原型链有一个终点,即:Object.prototype.__proto__ === null;当我们访问一个属性值的时候, 它会沿着原型链向上查找, 直到原型链终点:null.

  function Person() {}
  var person1 = new Person();

  console.log(Person.prototype); // {constructor: ƒ}
  console.log(person1.__proto__); // {constructor: ƒ}
  console.log(person1.__proto__ === Person.prototype); // true
yuan() {
      function Person() {}
      var person1 = new Person();

      console.log(Person.prototype); // {constructor: ƒ}
      console.log(person1.__proto__); // {constructor: ƒ}
      console.log(person1.__proto__ === Person.prototype); // true

      console.log(Person.prototype); // {constructor: ƒ}
      console.log(person1.__proto__.constructor.prototype); // {constructor: ƒ}
      console.log(person1.__proto__.constructor.prototype === Person.prototype); // true

      console.log(Person.prototype.__proto__); // {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ,…}
      console.log(person1.__proto__.__proto__); // {constructor: ƒ, __defineGetter__: ƒ, __defineSetter__: ƒ, hasOwnProperty: ƒ, __lookupGetter__: ƒ,…}
      console.log(Person.prototype.__proto__ === person1.__proto__.__proto__); // true

      console.log(Person.prototype.__proto__.__proto__); // null
      console.log(person1.__proto__.__proto__.__proto__); // null
      console.log(
        Person.prototype.__proto__.__proto__ ===
          person1.__proto__.__proto__.__proto__
      ); // true
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值