JavaScript中的原型到底该如何理解?

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JavaScript作为一个基于原型的OOP,和我们熟知的基于类的面向对象编程语言有很大的差异。如果不理解其中的本质含义,则无法深入理解JavaScript的诸多特性,以及由此产生的诸多“坑”。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"原型"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"概念"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在讨论“原型”的概念之前,我们先来讨论一下“类”,也就是Java、C++等语言所使用的概念。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在基于类的编程语言中,都要先抽象出一个“类”,用来统一表示同一种对象。然后用这个抽象类创建出一个个实例(泛化),也就是对象object。最后,类和类之间通过组合、继承等特性共建出一个可以互动的系统,从而用这套人为创建的系统来模拟、操纵现实中的物理世界。它的三大特性为:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"封装"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"继承"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"多态"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然而,在原型概念中,有很多不同之处。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于原型的编程范式提倡程序设计者关注实例对象的一系列行为,然后根据行为的不同划分出不同的原型,而不是事先抽象出一个类,再关注具体的对象。它最大的特点是可以动态修改对象的行为,具有高度灵活性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"如果把基于类的对象称为“自上而下”式的顶层设计,那么基于原型的对象则可以被称为“自下而上”式的动态演化。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这就像专业人士可能喜欢在看到老虎的时候,喜欢用猫科豹属豹亚种来描述它,但是对一些不那么正式的场合,“大猫”可能更为接近直观的感受一些(插播一个冷知识:比起老虎来,美洲狮在历史上相当长时间都被划分为猫科猫属,所以性格也跟猫更相似,比较亲人)。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于原型的面向对象系统通过“复制”的方式来创建新对象,这实际上就是创建一个全新的对象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原型系统的“复制”操作有两种实现思路:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"并不是真正的复制一个对象,而是使新对象持有一个原型的引用;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"切实的复制一个对象,复制对象和被复制对象再无任何关联。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"zerowidth"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JavaScript选择了前一种复制方式。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"JavaScript中的原型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抛开一切复杂的语法规则,JavaScript的原型系统的实质只有两条:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果对象都有私有字段[[prototype]],那它就是对象的原型;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"读一个属性,如果对象自身没有,则继续访问对象的原型,直到找到或者原型为空为止。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从 ES6 以来,JavaScript提供了一系列内置函数,以便更直接地访问操纵原型,分别为:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Object.create"}]},{"type":"text","text":" 根据指定的原型创建新对象,原型可以是"},{"type":"codeinline","content":[{"type":"text","text":"null"}]},{"type":"text","text":";"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Object.getPrototypeOf"}]},{"type":"text","text":" 获得一个对象的原型;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Object.setPrototypeOf"}]},{"type":"text","text":" 设置一个对象的原型。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"利用这三个方法,我们完全可以抛开基于类的面向对象思维,用原型的概念实现抽象和复用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"// 作为“原型”的猫\nvar cat = {\n say(){\n console.log(\"meow~\");\n },\n jump(){\n console.log(\"jump\");\n }\n}\n\n// 作为“原型”的老虎\nvar tiger = Object.create(cat, {\n say:{\n writable:true,\n configurable:true,\n enumerable:true,\n value:function(){\n console.log(\"roar!\");\n }\n }\n})\n\n\nvar anotherCat = Object.create(cat);\n\nanotherCat.say();\n// meow~\n\nvar anotherTiger = Object.create(tiger);\n\nanotherTiger.say();\n// jump"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"早期版本中的原型"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ES3"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在ES3之前的版本中,“类”的定义只是对象的一个私有属性 [[class]],语言标准为内置类型Number、String、Date等指定了[[class]]属性,以表示它们的类。语言使用者唯一可以访问[[class]]属性的方式是"},{"type":"codeinline","content":[{"type":"text","text":" Object.prototype.toString"}]},{"type":"text","text":" 。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var o = new Object;\nvar n = new Number;\nvar s = new String;\nvar b = new Boolean;\nvar d = new Date;\n\nconsole.log([o, n, s, b, d].map(v => Object.prototype.toString.call(v))); \n// 0: \"[object Object]\"\n// 1: \"[object Number]\"\n// 2: \"[object String]\"\n// 3: \"[object Boolean]\"\n// 4: \"[object Date]\""}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ES5"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从ES5开始,[[class]] 私有属性被 "},{"type":"codeinline","content":[{"type":"text","text":"Symbol.toStringTag"}]},{"type":"text","text":" 代替,"},{"type":"codeinline","content":[{"type":"text","text":"Object.prototype.toString"}]},{"type":"text","text":" 的意义从命名上不再跟 "},{"type":"codeinline","content":[{"type":"text","text":"class"}]},{"type":"text","text":" 相关。我们甚至可以自定义 "},{"type":"codeinline","content":[{"type":"text","text":"Object.prototype.toString"}]},{"type":"text","text":" 的行为,以下代码展示了使用Symbol."},{"type":"codeinline","content":[{"type":"text","text":"toStringTag"}]},{"type":"text","text":"来自定义 "},{"type":"codeinline","content":[{"type":"text","text":"Object.prototype.toString"}]},{"type":"text","text":" 的行为:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var o = { [Symbol.toStringTag]: \"MyObject\" }\nconsole.log(o + \"\");\n// [object MyObject]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"new"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"new"}]},{"type":"text","text":" 运算符是JavaScript面向对象体系中非常重要的一员。在ES6之前,"},{"type":"codeinline","content":[{"type":"text","text":"new"}]},{"type":"text","text":" 和函数基本上撑起了JavaScript的对象系统。"},{"type":"codeinline","content":[{"type":"text","text":"new"}]},{"type":"text","text":" 运算接受一个构造器和一组参数,实际上做了这些事:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以构造器的 "},{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":" 属性(注意与私有字段[[prototype]]的区分)为原型,创建新对象;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"将 "},{"type":"codeinline","content":[{"type":"text","text":"this"}]},{"type":"text","text":" 和调用参数传给构造器,执行;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果构造器返回的是对象,则返回;否则返回第一步创建的对象。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"实际上,它提供了两种方式用于操作对象的属性,其一是在构造器上添加属性;其二实在构造器的"},{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":"上添加属性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"function c1(){\n this.p1 = 1;\n this.p2 = function(){\n console.log(this.p1);\n }\n} \nvar o1 = new c1;\no1.p2();\n\n\nfunction c2(){\n}\nc2.prototype.p1 = 1;\nc2.prototype.p2 = function(){\n console.log(this.p1);\n}\n\nvar o2 = new c2;\no2.p2();"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在没有"},{"type":"codeinline","content":[{"type":"text","text":"Object.create"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"Object.setPrototypeOf"}]},{"type":"text","text":" 的早期版本中,"},{"type":"codeinline","content":[{"type":"text","text":"new"}]},{"type":"text","text":" 运算是唯一一个可以指定[[prototype]]的方法(当时的mozilla提供了私有属性"},{"type":"text","marks":[{"type":"strong"}],"text":"proto"},{"type":"text","text":";但在目前,大多数浏览器已经支持这一私有属性__proto__)。所以,当时已经有人试图用它来代替后来的"},{"type":"codeinline","content":[{"type":"text","text":" Object.create"}]},{"type":"text","text":",我们甚至可以用它来实现一个 "},{"type":"codeinline","content":[{"type":"text","text":"Object.create"}]},{"type":"text","text":" 的不完整的polyfill:"}]},{"type":"codeblock","attrs":{"lang":"js"},"content":[{"type":"text","text":"Object.create = function(prototype) {\n var cls = function(){}\n cls.prototype = prototype;\n return new cls;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这段代码创建了一个空函数作为类,并把传入的原型挂在了它的 "},{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":" 上,最后创建了一个它的实例,根据 "},{"type":"codeinline","content":[{"type":"text","text":"new"}]},{"type":"text","text":" 的行为,这将产生一个以传入的第一个参数为原型的对象。这个函数无法做到与原生的 "},{"type":"codeinline","content":[{"type":"text","text":"Object.create"}]},{"type":"text","text":" 一致,一个是不支持第二个参数,另一个是不支持 "},{"type":"codeinline","content":[{"type":"text","text":"null"}]},{"type":"text","text":" 作为原型,所以放到今天意义已经不大了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"ES6中的类class"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ES6中, "},{"type":"codeinline","content":[{"type":"text","text":"class"}]},{"type":"text","text":" 成为JavaScript官方支持的关键字,可以像其他语言一样定义类,并且还支持 "},{"type":"codeinline","content":[{"type":"text","text":"extends"}]},{"type":"text","text":" 关键字来实现继承,"},{"type":"codeinline","content":[{"type":"text","text":"setter"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"getter"}]},{"type":"text","text":" 也支持。至此,基于类的编程范式成为JavaScript官方支持的编程范式。如下所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"class Rectangle {\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n // Getter\n get area() {\n return this.calcArea();\n }\n set area(area) {\n this.area = area;\n }\n // Method\n calcArea() {\n return this.height * this.width;\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"实现继承的用法:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"class Animal { \n constructor(name) {\n this.name = name;\n }\n \n speak() {\n console.log(this.name + ' makes a noise.');\n }\n}\n\nclass Dog extends Animal {\n constructor(name) {\n super(name); // call the super class constructor and pass in the name parameter\n }\n\n speak() {\n console.log(this.name + ' barks.');\n }\n}\n\nlet d = new Dog('Mitzie');\nd.speak(); // Mitzie barks."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以从此以后,我们都应该使用 "},{"type":"codeinline","content":[{"type":"text","text":"class"}]},{"type":"text","text":" 关键字正正经经地定义类,而不是用各种怪异的手法模拟对象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"原型链"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JavaScript的每个对象都有一个指向其原型对象的关系链,当试图访问一个属性时,它不仅仅在对象上搜寻,而且还会在它的原型上搜寻,以及原型的原型上搜寻,直到找到属性或者达到此链条的顶端,这就是JavaScript的原型链,用来实现"},{"type":"text","marks":[{"type":"strong"}],"text":"继承"},{"type":"text","text":"的核心逻辑。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"函数对象和构造器对象"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除过上面对于JavaScript对象的的一般分类方法,还有另一个角度,就是用对象来模拟函数和构造器。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"JavaScript中函数对象的定义是:具有[[call]]私有字段的对象;构造器对象的定义是:具有私有字段[[construct]]的对象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">JavaScript用对象模拟函数的设计代替了一般编程语言中的函数,它们可以像其它语言的函数一样被调用、传参。任何宿主只要提供了“具有[[call]]私有字段的对象”,就可以被 JavaScript 函数调用语法支持。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以在JavaScript中,任何对象只要实现了[[call]],它就是一个函数对象,可以去作为函数被调用。而如果它能实现[[construct]],它就是一个构造器对象,可以作为构造器被调用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外,还有非常重要的一点:用"},{"type":"codeinline","content":[{"type":"text","text":"function"}]},{"type":"text","text":"关键字创建的函数,既是函数(对象),又是构造器(对象)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于宿主和内置对象来说,[[call]](作为函数被调用)的行为和[[construct]](作为构造器被调用)的行为可能存在些许差异。而用户使用 "},{"type":"codeinline","content":[{"type":"text","text":"function"}]},{"type":"text","text":" 语法或者"},{"type":"codeinline","content":[{"type":"text","text":"Function"}]},{"type":"text","text":"构造器创建的对象来说,[[call]]和[[construct]]行为总是一致的。但是,在ES6之后用 "},{"type":"codeinline","content":[{"type":"text","text":"=>"}]},{"type":"text","text":" 语法创建的函数仅仅是函数,它们无法被当作构造器使用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"js"},"content":[{"type":"text","text":"function f(){\n return 1;\n}\nvar v = f(); //把f作为函数调用\nvar o = new f(); //把f作为构造器调用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面这段代码,它的[[construct]]的执行过程如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"以 "},{"type":"codeinline","content":[{"type":"text","text":"Object.protoype"}]},{"type":"text","text":" 为原型创建一个新对象;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"以新对象为 "},{"type":"codeinline","content":[{"type":"text","text":"this"}]},{"type":"text","text":",执行函数的[[call]];"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"如果[[call]]的返回值是对象,那么,返回这个对象;否则返回第一步创建的新对象。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"prototype(显式原型)和_"},{"type":"text","marks":[{"type":"italic"}],"text":"proto"},{"type":"text","text":"_(隐式原型)"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"prototype"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每一个函数在创建之后都会拥有一个名为"},{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":"的属性,这个属性指向函数的原型对象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Note:通过Function.prototype.bind方法构造出来的函数是个例外,它没有prototype属性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那么,prototype的作用是什么呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">ECMAScript does not use classes such as those in C++, Smalltalk, or Java. Instead, objects may be created in various ways including via a literal notation or via constructors which create objects and then execute code that initializes all or part of them by assigning initial values to their properties. Each constructor is a function that has a property named “prototype” that is used to implement prototype-based inheritance and shared properties. Objects are created by using constructors in new expressions; for example, new Date(2009,11) creates a new Date object. ----"},{"type":"link","attrs":{"href":"https://link.zhihu.com/?target=http%3A//www.ecma-international.org/ecma-262/5.1/%23sec-4.2.1","title":""},"content":[{"type":"text","text":"ECMAScript Language Specification"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一句话,prototype用来实现基于原型的继承与属性的共享。prototype属性只有Function对象有。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"_"},{"type":"text","marks":[{"type":"italic"}],"text":"proto"},{"type":"text","text":"_"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"遵循ECMAScript标准,someObject.[[Prototype]] 符号是用于指向 someObject 的原型。从 ECMAScript 6 开始,[[Prototype]] 可以通过 Object.getPrototypeOf() 和 Object.setPrototypeOf() 访问器来访问。这个等同于 JavaScript 的非标准但许多浏览器实现的属性 "},{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":"。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每个实例对象( object )都有一个私有属性(称之为 "},{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":" )指向它的构造函数的原型对象( "},{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":" )。该原型对象也有一个自己的原型对象( "},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"proto"}]},{"type":"text","text":" ) ,层层向上直到一个对象的原型对象为 "},{"type":"codeinline","content":[{"type":"text","text":"null"}]},{"type":"text","text":"。根据定义,"},{"type":"codeinline","content":[{"type":"text","text":"null"}]},{"type":"text","text":" 没有原型,并作为这个原型链中的最后一个环节。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"也就是说:对每一个对象,"},{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":"是构成JavaScript对象基于原型链的继承关系的具体实现细节。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要注意的是,**JavaScript的函数function也是对象(Function的实例,也就是函数对象)。所以,function也有了"},{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":"属性**,指向Function.prototype。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用下面两张图演直观感受一下原型链的真实样子:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ab/ab4e90ce17e2595114c8974bcfc77509.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"牢记两点:"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - "},{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":"属性是对象所独有的;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - "},{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":"属性是函数所独有的;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - 因为函数也是一种对象,所以同时拥有"},{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":"属性和"},{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":"属性。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":"属性的作用是当访问一个对象的属性时,如果该对象内部不存在这个属性,那么就会去它的"},{"type":"codeinline","content":[{"type":"text","text":"__proto__"}]},{"type":"text","text":"所指向的那个对象里找,一直找,直到"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"proto"}]},{"type":"text","text":"属性为null,再往上找就相当于在null上取值,会报错。通过"},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong"}],"text":"proto"}]},{"type":"text","text":"属性将对象的继承关系连接起来的这条链路即原型链。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"prototype"}]},{"type":"text","text":"属性的作用是让该函数所实例化的对象们都可以找到公用的属性和方法,即"},{"type":"codeinline","content":[{"type":"text","text":"f1.__proto__ === Foo.prototype"}]},{"type":"text","text":"。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章