原型和继承

原型是什么 

简而言之  原型是函数的一个对象属性 创建一个函数的时候  会默认添加一个Prototype属性

我们接下来简单的实现一下

 function test(name) {

      this.name = name

    }

  console.dir(test);

  console.log(test.prototype);

上述代码已经得到证实了

    // prototype  (原型 原型对象)

    // 1 是函数的一个属性

    // 2 是个对象

    // 3 创建函数的时候会默认添加Prototype属性

 __proto__又是什么呢 

    // __proto__  隐式原型

    // 对象的属性

    // 1 对象的一个属性

    // 2 指向构造函数的prototype

接下来我们来证实一下这几句话

var obj = new test('小明')

console.log(obj.__proto__);

console.log(obj.__proto__ === test.prototype);

 证明完成 

test.prototype也是一个对象  他也应该有__proto__ ,他指向的Object.prototype

也就是 

console.log(test.prototype.__proto__ === Object.prototype);  //true

 那么原型链是什么  ?

  // 所谓的原型链就是

     // obj {

    //   __proto__:test.prototype={

    //     __proto__:Object.prototype={

    //         __proto__:null

    //      }

    //   }

    // }

并且给test.prototype中一个属性c  可以在obj中访问到  给Object.prototype一个属性 也可以在obj中访问到

继承是什么

// 继承方法

    const parent = {

      value: 2,

      method() {

        console.log(this);

        return this.value + 1

      }

    }

    console.log(parent.method());//3 不难理解

    const child = {

      __proto__: parent,

    }

    console.log(child.method());//3  this 指向child  但是属性中没有value 在原型链上查找 最后找到原型的value

    child.value = 4

    console.log(child.method());//child有value属性了 发生属性遮蔽

相关推荐

  1. 继承原型

    2024-02-01 08:02:01       31 阅读
  2. 原型继承

    2024-02-01 08:02:01       34 阅读
  3. 如何在原型中实现继承多态

    2024-02-01 08:02:01       33 阅读
  4. c++中的单继承、多继承虚拟继承

    2024-02-01 08:02:01       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-01 08:02:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-01 08:02:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-01 08:02:01       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-01 08:02:01       20 阅读

热门阅读

  1. electron从入门到打包exe

    2024-02-01 08:02:01       44 阅读
  2. 本地部署whisper模型(语音转文字)

    2024-02-01 08:02:01       36 阅读
  3. SummaryWriter函数用法

    2024-02-01 08:02:01       31 阅读
  4. Spring中用到的设计模式

    2024-02-01 08:02:01       31 阅读
  5. 实用Python定时点击Chrome网页按钮

    2024-02-01 08:02:01       37 阅读
  6. Vue之前端Broadcast Channel API的简单使用

    2024-02-01 08:02:01       41 阅读
  7. Day05-Linux bash核心介绍及目录命令讲解

    2024-02-01 08:02:01       37 阅读