0
点赞
收藏
分享

微信扫一扫

下班前几分钟,我彻底弄懂了JavaScript的原型与原型链

前言

JavaScript 原型与原型链历来都是面试的重点,也是难点,理解起来没有那么容易。


正文

理解原型的几个要点,能更容易理解原型这个概念:


1、所有的引用类型(数组、对象、函数)可以自由扩展属性(除null以外);


2、所有的引用类型都有一个“__proto__”属性(隐式原型,是一个对象);


3、所有的函数都有一个“prototype”属性(显式原型,是一个对象);


4、所有引用类型的“__proto__”属性指向它的构造函数的“prototype”属性;


5、当访问一个对象的属性时,如果这个对象本身不存在该属性,则就会去它的“__proto__”属性(也就是它的原型)中去寻找。


注意:在JavaScript中,一切皆对象。


原型


举个简单例子,理解原型


// 定义一个构造函数

function Fn(name,age){

this.name = name;

this.age = age;

}


/*

由以上第3点可知,所有的函数都有一个prototype属性,这个属性是一个普通对象

由以上第1点可知,所有的对象可以通过prototype自由扩展属性(除了null)

*/

Fn.prototype = {

// prototype对象里面又有其他的属性

showName: function(){

 console.log("My name is " + this.name);

},

showAge: function(){

 console.log("I'm " + this.age + " years old");

}

}


let obj = new Fn('leo',18)

/*

当访问一个对象的属性时,如果该对象本身不存在这个属性,

那么就会去它的构造函数的prototype属性中去找

*/

obj.showName();   // My name is leo


obj.showAge();   // I'm 18 years old

这就是原型,很好理解。那为什么要使用原型呢?


试想如果我们要通过 Fn() 来创建多个对象,会这么写


function Fn(name,age){

this.name =name;

this.age = age;

this.showName = function(){

 console.log("My name is " + this.name);

},

this.showAge = function(){

 console.log("I'm " + this.age + " years old");

}

}

那么我们创建出来的每一个对象,里面都会包含showName和showAge方法,这样就会占用很多的资源。而通过原型来实现的话,只需要在构造函数里面给 prototype 属性赋值,把扩展的方法写在Fn.prototype 属性里面,这样通过该构造函数构造出来的每个对象都可以使用 prototype 属性里面的 showName、showAge 方法,节省了不少资源。


原型链


理解了原型,那么原型链就很好理解了。


由第5点可知,当访问一个对象的属性时,如果该对象本身不存在这个属性,那么就会去它构造函数的“prototype”属性(即原型)中去寻找,又因为“prototype”属性也是一个对象,所以它也有一个“__proto__”属性


// 定义一个构造函数

function Foo(name,age){

   this.name = name;

   this.age = age;

}

Object.prototype.toString = function(){

console.log("My name is " + this.name + " And I'm " + this.age + " years old");

}

let fn = new Foo('leo',18);

fn.toString();   // My name id leo And I'm 18 years old


console.log(fn.__proto__ === Foo.prototype);   // true


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


console.log(fn.toString === Foo.prototype.__proto__.toString);   // true


// 原型链机制(层层往上找),fn.toString实际访问的是Object.prototype.toString

console.log(fn.toString === Object.prototype.toString);   // true


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



由上面的例子,得到如下图(图片来源网络)




1、fn 实例的构造函数是 Foo(),因此


fn.__ proto__ === Foo.prototype

2、Foo.prototype 也是一个对象(拥有 __proto__ 属性),它的构造函数是 Object(),因此


Foo.prototype.__proto__ === Object.prototype

3、toString() 是在 Object.prototype 里面的,当调用的对象本身不存在该方法时,它会一层一层地往上去找,一直到null为止。

4、fn 调用 toString() 时,发现 fn 本身没有这个方法,于是就去 Foo.prototype 中找,发现还是没有,然后继续往 Object.prototype 中找,找到了,就调用 Object.prototype 中的 toString() 方法。

这就是原型链,fn 能够调用 Object.prototype 中的方法正是因为原型链的机制。


注意:在使用原型的时候,一般推荐将需要扩展的方法写在构造函数的 prototype 属性中。


理解了上面的例子之后,我们再来看以下这个图(图片来源网络)




结合上面的例子进行分析


fn:自定义对象


Foo:自定义函数


1、自定义函数(new) --> 自定义对象


let fn = new Foo('leo',18);

2、自定义对象 (__proto__)--> 自定义函数原型


fn.__ proto__ === Foo.prototype

3、Js中,一切皆对象,自定义函数也不了例外,即也存在__proto__属性,而自定义函数的构造函数是 Function(),因此得到

http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E7%9A%84%E5%AE%A2%E6%9C%8D%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E3%80%9017166918222%E3%80%91XHZ

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%B8%B8%E6%88%8Fapp%E4%B8%8B%E8%BD%BD%E3%80%9017166918222%E3%80%91Qxs

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B7%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91S47

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%97%97%E4%B8%8B%E8%89%BA%E4%BA%BA%E3%80%9017166918222%E3%80%91M1G

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%B9%B3%E5%8F%B0%E3%80%9017166918222%E3%80%91Tdn

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91n88

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E4%B8%8B%E5%88%86%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91ucZ

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90app%E4%B8%8B%E8%BD%BD%E3%80%9017166918222%E3%80%91uhq

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E7%9A%84%E5%AE%A2%E6%9C%8D%E7%94%B5%E8%AF%9D%E6%98%AF%E5%A4%9A%E5%B0%91%3F%E3%80%9017166918222%E3%80%91uU3

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E4%B8%8B%E5%88%86%E5%AE%A2%E6%9C%8D%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91hlt

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E7%94%B5%E8%AF%9D%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91n9Q

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E6%98%AF%E5%A4%9A%E5%B0%91%E3%80%9017166918222%E3%80%91aYe

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E4%B8%8A%E4%B8%8B%E5%88%86%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91A2v

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%98%E7%BD%91%E3%80%9017166918222%E3%80%91MAc

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E8%81%94%E7%B3%BB%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91f78

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91SPM

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91Y4Y

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E7%8E%B0%E5%9C%BA%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91l04

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E3%80%9017166918222%E3%80%91jKW

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8app%E4%B8%8B%E8%BD%BD%E3%80%9017166918222%E3%80%91D2R

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91kKZ

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91QZs

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91Qm0

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%9A%84%E6%B3%A8%E5%86%8C%E5%AE%98%E7%BD%91%E6%98%AF%E5%A4%9A%E5%B0%91%3F%E3%80%9017166918222%E3%80%91V3l

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%B8%B8%E6%88%8F%E7%BD%91%E5%9D%80%E3%80%9017166918222%E3%80%91CoT

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91zC7

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%B3%A8%E5%86%8C%E4%BC%9A%E5%91%98%E8%B4%A6%E5%8F%B7%E8%A6%81%E6%89%BE%E8%B0%81%E3%80%9017166918222%E3%80%91qiz

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E3%80%9017166918222%E3%80%91e3H

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%80%8E%E4%B9%88%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91cWO

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E6%B3%A8%E5%86%8C%E9%9D%A0%E8%B0%B1%E3%80%9017166918222%E3%80%91X9d

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E5%88%86%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91osX

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%9A%84%E5%AE%A2%E6%9C%8D%E5%A6%82%E4%BD%95%E8%81%94%E7%B3%BB%3F%E3%80%9017166918222%E3%80%91k74

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B7%E5%AE%A2%E6%9C%8D%E5%91%A2%3F%E3%80%9017166918222%E3%80%91zNv

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8APP%E3%80%9017166918222%E3%80%91ENI

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%80%8E%E4%B9%88%E5%BC%80%E6%88%B7%E3%80%9017166918222%E3%80%91Upl

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E3%80%9017166918222%E3%80%91MQG

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%B9%B3%E5%8F%B0%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91Yng

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%B9%B3%E5%8F%B0%E5%A6%82%E4%BD%95%E3%80%9017166918222%E3%80%91j77

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%B9%B3%E5%8F%B0%E6%AD%A3%E8%A7%84%E5%90%97%3F%E3%80%9017166918222%E3%80%91NAh

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E3%80%9017166918222%E3%80%91aeF

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E4%B8%8B%E5%88%86%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91jZ0

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E4%B8%8B%E5%88%86%E5%AE%A2%E6%9C%8DQQ%E3%80%9017166918222%E3%80%91fa7

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%80%8E%E4%B9%88%E4%B8%8A%E4%B8%8B%E5%88%86%E3%80%9017166918222%E3%80%91Z5E

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E4%B8%8B%E5%88%86%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91RUm

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B7%E3%80%9017166918222%E3%80%91cU1

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%B3%A8%E5%86%8C%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91k49

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8DQQ%E3%80%9017166918222%E3%80%91fpx

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%A6%81%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%9A%84%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91FsX

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8B%E8%BD%BD%E7%BD%91%E5%9D%80%E3%80%9017166918222%E3%80%91Rdc

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91NYD

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%B9%B3%E5%8F%B0%E9%9D%A0%E8%B0%B1%E5%90%97%E3%80%9017166918222%E3%80%91rq1

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%B3%A8%E5%86%8C%E5%AE%98%E7%BD%91%E3%80%9017166918222%E3%80%91q03

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%98%E7%BD%91%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91C90

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F%E3%80%9017166918222%E3%80%91f0m

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E5%91%A2%3F%E3%80%9017166918222%E3%80%91F9F

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%98%E7%BD%91%E3%80%9017166918222%E3%80%91g2N

​​http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%95%85%E8%81%8A%E3%80%9017166918222%E3%80%91FI6​​

Foo.__proto__ === Function.prototype

4、Object() 的构造函数也是 Function(),因此得到


Object.__proto__ === Function.prototype

5、Function.prototype 也是一个对象,也有__proto__属性,因此得到


Function.prototype.__proto__ === Objct.prototype


// Function 也是一个对象,也有__proto__属性,也是一个对象,也有__proto__属性

Function.__proto__.__proto__ === Objct.prototype

6、Object.prototype 也是一个对象,也有__proto__属性,不同的是

————————————————

版权声明:本文为CSDN博主「前端不释卷leo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_41809113/article/details/122720550前言

JavaScript 原型与原型链历来都是面试的重点,也是难点,理解起来没有那么容易。


正文

理解原型的几个要点,能更容易理解原型这个概念:


1、所有的引用类型(数组、对象、函数)可以自由扩展属性(除null以外);


2、所有的引用类型都有一个“__proto__”属性(隐式原型,是一个对象);


3、所有的函数都有一个“prototype”属性(显式原型,是一个对象);


4、所有引用类型的“__proto__”属性指向它的构造函数的“prototype”属性;


5、当访问一个对象的属性时,如果这个对象本身不存在该属性,则就会去它的“__proto__”属性(也就是它的原型)中去寻找。


注意:在JavaScript中,一切皆对象。


原型


举个简单例子,理解原型


// 定义一个构造函数

function Fn(name,age){

this.name = name;

this.age = age;

}


/*

由以上第3点可知,所有的函数都有一个prototype属性,这个属性是一个普通对象

由以上第1点可知,所有的对象可以通过prototype自由扩展属性(除了null)

*/

Fn.prototype = {

// prototype对象里面又有其他的属性

showName: function(){

 console.log("My name is " + this.name);

},

showAge: function(){

 console.log("I'm " + this.age + " years old");

}

}


let obj = new Fn('leo',18)

/*

当访问一个对象的属性时,如果该对象本身不存在这个属性,

那么就会去它的构造函数的prototype属性中去找

*/

obj.showName();   // My name is leo

obj.showAge();   // I'm 18 years old

这就是原型,很好理解。那为什么要使用原型呢?


试想如果我们要通过 Fn() 来创建多个对象,会这么写


function Fn(name,age){

this.name =name;

this.age = age;


this.showName = function(){

 console.log("My name is " + this.name);

},

this.showAge = function(){

 console.log("I'm " + this.age + " years old");

}

}

那么我们创建出来的每一个对象,里面都会包含showName和showAge方法,这样就会占用很多的资源。而通过原型来实现的话,只需要在构造函数里面给 prototype 属性赋值,把扩展的方法写在Fn.prototype 属性里面,这样通过该构造函数构造出来的每个对象都可以使用 prototype 属性里面的 showName、showAge 方法,节省了不少资源。


原型链


理解了原型,那么原型链就很好理解了。


由第5点可知,当访问一个对象的属性时,如果该对象本身不存在这个属性,那么就会去它构造函数的“prototype”属性(即原型)中去寻找,又因为“prototype”属性也是一个对象,所以它也有一个“__proto__”属性


// 定义一个构造函数

function Foo(name,age){

   this.name = name;

   this.age = age;

}

Object.prototype.toString = function(){

console.log("My name is " + this.name + " And I'm " + this.age + " years old");

}

let fn = new Foo('leo',18);

fn.toString();   // My name id leo And I'm 18 years old


console.log(fn.__proto__ === Foo.prototype);   // true


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

http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E7%BB%8F%E7%90%86%E7%9A%84%E7%94%B5%E8%AF%9D%E6%98%AF%E5%A4%9A%E5%B0%91%E3%80%9017166918222%E3%80%91kxB

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91DFo

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%8A%95%E8%AF%89%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91NJ3

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%8A%95%E8%AF%89%E7%83%AD%E7%BA%BF%E3%80%9017166918222%E3%80%91vAM

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B7%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91F75

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E6%B8%B8%E6%88%8Fapp%E4%B8%8B%E8%BD%BD%E3%80%9017166918222%E3%80%91CxB

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E4%B8%8B%E5%88%86%E3%80%9017166918222%E3%80%91D51

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%B8%8A%E4%B8%8B%E5%88%86%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91qdw

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E8%B4%9F%E8%B4%A3%E4%BA%BA%E3%80%9017166918222%E3%80%91EfJ

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%BD%91%E5%9D%80%E3%80%9017166918222%E3%80%91evg

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%9A%84%E6%B3%A8%E5%86%8C%E5%AE%98%E7%BD%91%E6%98%AF%E5%A4%9A%E5%B0%91%3F%E3%80%9017166918222%E3%80%91Dvl

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E8%81%94%E7%B3%BB%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91Q6R

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E3%80%9017166918222%E3%80%91vca

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E6%80%8E%E4%B9%88%E5%BC%80%E6%88%B7%E3%80%9017166918222%E3%80%91eO6

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E7%99%BB%E5%BD%95%E5%B9%B3%E5%8F%B0%E3%80%9017166918222%E3%80%91y94

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E6%B8%B8%E6%88%8F%E7%BD%91%E5%9D%80%E3%80%9017166918222%E3%80%91KIn

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E7%9C%9F%E4%BA%BA%E5%B9%B3%E5%8F%B0%E3%80%9017166918222%E3%80%91q23

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E7%BA%BF%E4%B8%8A%E5%A8%B1%E4%B9%90%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91V55

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90app%E4%B8%8B%E8%BD%BD%E3%80%9017166918222%E3%80%91ybB

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91At1

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91RAB

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91k27

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B7%E5%AE%A2%E6%9C%8D%E5%91%A2%3F%E3%80%9017166918222%E3%80%91HBV

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91Jt2

​​http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E4%BC%9A%E5%91%98%E6%B3%A8%E5%86%8C%E8%B4%A6%E5%8F%B7%E3%80%9017166918222%E3%80%91mb3​​

console.log(fn.toString === Foo.prototype.__proto__.toString);   // true


// 原型链机制(层层往上找),fn.toString实际访问的是Object.prototype.toString

console.log(fn.toString === Object.prototype.toString);   // true


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



由上面的例子,得到如下图(图片来源网络)




1、fn 实例的构造函数是 Foo(),因此


fn.__ proto__ === Foo.prototype

2、Foo.prototype 也是一个对象(拥有 __proto__ 属性),它的构造函数是 Object(),因此


Foo.prototype.__proto__ === Object.prototype

3、toString() 是在 Object.prototype 里面的,当调用的对象本身不存在该方法时,它会一层一层地往上去找,一直到null为止。

4、fn 调用 toString() 时,发现 fn 本身没有这个方法,于是就去 Foo.prototype 中找,发现还是没有,然后继续往 Object.prototype 中找,找到了,就调用 Object.prototype 中的 toString() 方法。

这就是原型链,fn 能够调用 Object.prototype 中的方法正是因为原型链的机制。


注意:在使用原型的时候,一般推荐将需要扩展的方法写在构造函数的 prototype 属性中。


理解了上面的例子之后,我们再来看以下这个图(图片来源网络)




结合上面的例子进行分析


fn:自定义对象


Foo:自定义函数


1、自定义函数(new) --> 自定义对象


let fn = new Foo('leo',18);

2、自定义对象 (__proto__)--> 自定义函数原型


fn.__ proto__ === Foo.prototype

3、Js中,一切皆对象,自定义函数也不了例外,即也存在__proto__属性,而自定义函数的构造函数是 Function(),因此得到


Foo.__proto__ === Function.prototype

4、Object() 的构造函数也是 Function(),因此得到

http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E3%80%9017166918222%E3%80%91k8i

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91vlX

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E6%98%AF%E5%A4%9A%E5%B0%91%E3%80%9017166918222%E3%80%91t3M

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B7%E5%AE%A2%E6%9C%8D%E5%91%A2%3F%E3%80%9017166918222%E3%80%91a0z

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%B0%81%E7%9F%A5%E9%81%93%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E7%9A%84%E5%AE%A2%E6%9C%8D%E8%AF%A5%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%3F%E3%80%9017166918222%E3%80%91hKK

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91c2u

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E7%94%B5%E8%AF%9D%E6%80%8E%E4%B9%88%E8%81%94%E7%B3%BB%E3%80%9017166918222%E3%80%91X0m

​​http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%85%AC%E5%8F%B8%E8%81%94%E7%B3%BB%E6%96%B9%E5%BC%8F%E3%80%9017166918222%E3%80%91njj​​

Object.__proto__ === Function.prototype

5、Function.prototype 也是一个对象,也有__proto__属性,因此得到


Function.prototype.__proto__ === Objct.prototype


// Function 也是一个对象,也有__proto__属性,也是一个对象,也有__proto__属性

Function.__proto__.__proto__ === Objct.prototype

6、Object.prototype 也是一个对象,也有__proto__属性,不同的是

————————————————

版权声明:本文为CSDN博主「前端不释卷leo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_41809113/article/details/122720550

http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E3%80%9017166918222%E3%80%91iri

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E6%80%8E%E4%B9%88%E5%BC%80%E6%88%B7%E3%80%9017166918222%E3%80%91t9a

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E5%AE%A2%E6%9C%8D%E5%BE%AE%E7%94%B5%E3%80%9017166918222%E3%80%91ajp

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E5%BC%80%E6%88%B7%E3%80%9017166918222%E3%80%91SLd

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E7%BD%91%E5%9D%80%E3%80%9017166918222%E3%80%91iAt

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E4%B8%8A%E4%B8%8B%E5%88%86%E5%AE%A2%E6%9C%8D%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91wn5

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E5%BE%AE%E4%BF%A1%E3%80%9017166918222%E3%80%91Z44

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E6%89%8B%E6%9C%BA%E7%89%88%E4%B8%8B%E8%BD%BD%E3%80%9017166918222%E3%80%91gX5

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91QUu

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E5%BC%80%E6%88%B7%E3%80%9017166918222%E3%80%91ldL

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91Jd5

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E7%94%B5%E8%AF%9D%E3%80%9017166918222%E3%80%91tF2

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E7%99%BB%E5%BD%95%E5%B9%B3%E5%8F%B0%E3%80%9017166918222%E3%80%91YjT

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91Uew

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E6%B8%B8%E6%88%8F%E7%BD%91%E5%9D%80%E3%80%9017166918222%E3%80%91MrE

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%80%81%E8%A1%97%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%85%AC%E5%8F%B8%E3%80%9017166918222%E3%80%91xqq

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%9C%A8%E7%BA%BF%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91waf

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E6%B3%A8%E5%86%8C%E3%80%9017166918222%E3%80%91tRK

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E7%BB%8F%E7%90%86%E3%80%9017166918222%E3%80%91FqW

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E5%BC%80%E6%88%B7%E3%80%9017166918222%E3%80%91bXm

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E5%92%A8%E8%AF%A2%E3%80%9017166918222%E3%80%91cg6

">http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E5%8D%8E%E7%BA%B3%E5%A8%B1%E4%B9%90%E5%AE%A2%E6%9C%8D%E4%B8%AD%E5%BF%83%E3%80%9017166918222%E3%80%91FUu

​​http://physics.scu.edu.cn/search?catid=7,8,9,10,11,12,13&travel=true&q=%E8%B0%81%E7%9F%A5%E9%81%93%E5%8D%8E%E7%BA​​


举报

相关推荐

0 条评论