{"id":415,"date":"2019-01-12T23:59:49","date_gmt":"2019-01-12T15:59:49","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=415"},"modified":"2019-01-12T23:59:49","modified_gmt":"2019-01-12T15:59:49","slug":"ch-10-%e5%8e%9f%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=415","title":{"rendered":"Ch. 10 \u539f\u578b"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_75 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"\u986f\u793a\/\u96b1\u85cf\u5167\u5bb9\u76ee\u9304\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#Prototype\" >[[Prototype]]<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#%E8%A8%AD%E5%AE%9A%E8%88%87%E9%81%AE%E8%94%BD_Shadowing_%E7%89%B9%E6%80%A7\" >\u8a2d\u5b9a\u8207\u906e\u853d (Shadowing) \u7279\u6027<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D\" >\u300c\u985e\u5225\u300d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#%E3%80%8C%E5%BB%BA%E6%A7%8B%E5%99%A8%E3%80%8D\" >\u300c\u5efa\u69cb\u5668\u300d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#Prototypal_%E5%8E%9F%E5%9E%8B%E5%BC%8F_%E7%B9%BC%E6%89%BF\" >(Prototypal \u539f\u578b\u5f0f) \u7e7c\u627f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#%E6%AA%A2%E8%A6%96%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D%E9%97%9C%E4%BF%82\" >\u6aa2\u8996\u300c\u985e\u5225\u300d\u95dc\u4fc2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#%E7%89%A9%E4%BB%B6%E9%80%A3%E7%B5%90\" >\u7269\u4ef6\u9023\u7d50<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#%E9%80%A3%E7%B5%90%E4%BD%9C%E7%82%BA%E5%82%99%E6%8F%B4%E4%B9%8B%E7%94%A8%EF%BC%9F\" >\u9023\u7d50\u4f5c\u70ba\u5099\u63f4\u4e4b\u7528\uff1f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=415\/#%E8%A4%87%E7%BF%92\" >\u8907\u7fd2<\/a><\/li><\/ul><\/nav><\/div>\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prototype\"><\/span><strong>[[Prototype]]<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>JS \u7684\u7269\u4ef6\u4e2d\u6709\u4e00\u500b <code>[[Prototype]]<\/code> \u7279\u6027\uff0c\u8868\u793a\u5c0d\u53e6\u4e00\u500b\u7269\u4ef6\u7684\u53c3\u8003\uff0c\u9810\u8a2d\u88ab\u8ce6\u4e88\u4e00\u500b\u975e null \u503c\u3002<\/p>\n\n\n<p>\u7576\u53d6\u7528\u4e00\u500b\u7269\u4ef6\u7684\u7279\u6027\u6642 (\u4f8b\u5982 <code>myObj.a<\/code>)\uff0c\u7b2c\u4e00\u6b65\u5c31\u662f\u5148\u6aa2\u67e5\u7269\u4ef6\u662f\u5426\u6709 <code>a<\/code> \u7279\u6027\u3002\u7576\u4e0d\u5b58\u5728\u7684\u6642\u5019\uff0c<code>[[GET]]<\/code> \u4fbf\u6703\u5faa\u8457\u7269\u4ef6\u7684 <code>[[Prototype]]<\/code> \u7279\u6027\u5f80\u4e0a\u5c0b\u627e\u3002\u9019\u500b\u52d5\u4f5c\u6703\u6301\u7e8c\u9032\u884c\uff0c\u76f4\u5230\u4e32\u93c8\u7684\u5c3e\u7aef\u90fd\u9084\u6c92\u627e\u7684\uff0c\u5c31\u8fd4\u56de <code>undefined<\/code>\u3002<\/p>\n\n\n<p>\u6b63\u5e38\u7684\u4e32\u93c8\u5c3e\u7aef\u6703\u662f\u5167\u5efa\u7684 <code>Object.prototype<\/code> \u7269\u4ef6\u3002\u5728\u6b64\u6703\u6709\u5167\u5efa\u7684\u7279\u6027\u8207\u65b9\u6cd5\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A8%AD%E5%AE%9A%E8%88%87%E9%81%AE%E8%94%BD_Shadowing_%E7%89%B9%E6%80%A7\"><\/span>\u8a2d\u5b9a\u8207\u906e\u853d (Shadowing) \u7279\u6027<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u7576\u8a2d\u5b9a\u4e00\u500b\u7279\u6027\u5230\u7269\u4ef6\u4e0a\u6642\uff0c\u53ef\u80fd\u767c\u751f\u4ee5\u4e0b\u4e09\u7a2e\u60c5\u6cc1\uff0c\u4ee5\u5728 myObject \u52a0\u5165 foo \u70ba\u4f8b\uff1a<\/p>\n\n\n<ol class=\"wp-block-list\"><li>foo \u5728 myObject \u88ab\u65b0\u589e\u6216\u662f\u8986\u84cb\u3002<\/li><li>foo \u5728\u4e32\u93c8\u8f03\u9ad8\u8655\u88ab\u627e\u5230\uff0c\u800c\u4e14\u8a72\u7279\u6027\u8a2d\u5b9a\u70ba<strong>\u552f\u7368<\/strong>\uff0c\u90a3\u9ebc\u8a2d\u5b9a\u7684\u52d5\u4f5c\u6703\u88ab\u5ffd\u7565\uff0c\u82e5\u5728 <strong>strict mode<\/strong> \u4e2d\uff0c\u5247\u6703\u64f2\u51fa\u932f\u8aa4\u3002<\/li><li> foo \u5728\u4e32\u93c8\u8f03\u9ad8\u8655\u88ab\u627e\u5230\uff0c\u8a72\u540d\u7a31\u662f\u4e00\u500b\u8a2d\u503c\u5668 (setter)\uff0c\u5247\u6703\u547c\u53eb\u8a2d\u503c\u5668\uff0c\u4e0d\u6703\u6709 foo \u88ab\u65b0\u589e\u5230 myObject\u3002<\/li><\/ol>\n\n\n<p>\u5982\u679c\u5728\u7b2c 2\u30013 \u7a2e\u60c5\u6cc1\u4e2d\uff0c\u60f3\u8981\u8a2d\u503c\u7d66 myObject\uff0c\u9700\u8981\u6539\u70ba\u4f7f\u7528 <code>Object.defineProperty()<\/code>\u3002<\/p>\n\n\n<p>\u96b1\u542b\u7684\u906e\u853d\u6709\u6642\u6703\u5e36\u4f86\u610f\u60f3\u4e0d\u5230\u7684\u7d50\u679c\uff0c\u53c3\u8003\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var anotherObject = {\n    a: 2\n};\nvar myObject = Object.create( anotherObject );\nanotherObject.a; \/\/ 2\nmyObject.a; \/\/ 2\nanotherObject.hasOwnProperty( \"a\" ); \/\/ true\nmyObject.hasOwnProperty( \"a\" ); \/\/ false\nmyObject.a++; \/\/ oops, implicit shadowing!\nanotherObject.a; \/\/ 2\nmyObject.a; \/\/ 3\nmyObject.hasOwnProperty( \"a\" ); \/\/ true<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D\"><\/span><strong>\u300c\u985e\u5225\u300d<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>JS \u51fd\u5f0f\u4e2d\u5177\u6709\u4e00\u500b\u7279\u6b8a\u7279\u6027\uff0c\u516c\u958b\u3001\u4e0d\u53ef\u5217\u8209\uff0c\u53eb\u505a <code>prototype<\/code>\uff0c\u6703\u6307\u5411\u4e00\u500b\u4efb\u610f\u7684\u7269\u4ef6\u3002<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Foo() {\n    \/\/ ...\n}\nvar a = new Foo();\nObject.getPrototypeOf( a ) === Foo.prototype; \/\/ true<\/pre>\n\n\n<p>\u7576\u900f\u904e\u547c\u53eb <code>new Foo()<\/code> \u7522\u51fa\u4e00\u500b\u7269\u4ef6\u6642\uff0ca \u5167\u90e8\u7684 <code>[[Prototype]]<\/code> \u6703\u9023\u7d50\u5230 <code>Foo.prototype<\/code>\u3002<\/p>\n\n\n<p>\u8996\u89ba\u4e0a\u4f86\u8aaa\uff0c<code>[[Prototype]]<\/code> \u7684\u6a5f\u5236\u5982\u5716\u6240\u793a\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/github.com\/getify\/You-Dont-Know-JS\/raw\/master\/this%20%26%20object%20prototypes\/fig3.png\" alt=\"\" \/><figcaption>Credit: <a href=\"https:\/\/github.com\/getify\/You-Dont-Know-JS\/blob\/master\/this%20%26%20object%20prototypes\/ch5.md\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (\u5728\u65b0\u5206\u9801\u4e2d\u958b\u555f)\">https:\/\/github.com\/getify\/You-Dont-Know-JS\/blob\/master\/this%20%26%20object%20prototypes\/ch5.md<\/a><\/figcaption><\/figure><\/div>\n\n\n<p>\u4f7f\u7528\u300c\u7e7c\u627f\u300d\u4e00\u8a5e\uff0c\u6697\u793a\u8457\u62f7\u8c9d\u7684\u884c\u70ba\uff0c\u7136\u800c\u5728 JS \u4e2d\uff0c\u53ea\u662f\u5c07\u7269\u4ef6\u5efa\u7acb\u9023\u7d50\uff0c\u66f4\u5408\u9069\u7684\u8a5e\u5f59\u662f\u300c<strong>\u59d4\u6d3e (delegation)<\/strong>\u300d\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%80%8C%E5%BB%BA%E6%A7%8B%E5%99%A8%E3%80%8D\"><\/span><strong>\u300c\u5efa\u69cb\u5668\u300d<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p><code>Foo.prototype<\/code> \u9810\u8a2d\u6703\u5f97\u5230\u4e00\u500b\u516c\u958b\u3001\u4e0d\u53ef\u5217\u8209\u7684\u7279\u6027 <code>construct<\/code>\uff0c\u6703\u6307\u5411\u7269\u4ef6\u6240\u95dc\u806f\u7684\u51fd\u5f0f\uff0c\u5728\u9019\u88e1\u662f <code>Foo<\/code>\u3002<\/p>\n\n\n<p>\u7136\u800c <code>Foo<\/code> \u51fd\u5f0f\u672c\u8eab\u4e26\u4e0d\u662f\u4e00\u500b\u5efa\u69cb\u5668\uff0c\u53ea\u662f\u525b\u597d\u4ee5 <code>new<\/code> \u4f86\u547c\u53eb\u6642\uff0c\u6703\u5efa\u69cb\u51fa\u4e00\u500b\u7269\u4ef6\u3002\u5728\u67e5\u627e <code>a.constructor<\/code> \u6642\uff0c\u770b\u4f3c\u610f\u5473\u8457 a \u771f\u7684\u6709\u4e00\u500b\u5be6\u969b\u7684 <code>constructor<\/code>\uff0c\u4f46\u5be6\u969b\u4e0a\u53ea\u662f\u59d4\u6d3e\u7d66\u4e86\u4e0a\u982d\u7684 <code>Foo.prototype<\/code>\u3002<\/p>\n\n\n<p><code>.constructor<\/code> \u4e26\u4e0d\u662f\u4e00\u500b\u795e\u5947\u7684\u4e0d\u53ef\u8b8a\u7279\u6027\uff0c\u5b83\u4e0d\u53ef\u5217\u8209\uff0c\u4f46\u503c\u662f\u53ef\u4ee5\u5beb\u5165\u3001\u53ef\u4ee5\u88ab\u66f4\u6539\u7684\uff0c\u4e5f\u53ef\u4ee5\u5728\u4e32\u93c8\u4e2d\u7684\u4efb\u4f55\u7269\u4ef6\u4e2d\u96a8\u610f\u5730\u65b0\u589e\u3002\u56e0\u6b64\u9019\u500b\u7279\u6027\u7121\u6cd5\u771f\u6b63\u88ab\u4fe1\u4efb\uff0c\u4e0d\u53ef\u88ab\u5047\u8a2d\u70ba\u662f\u9810\u8a2d\u503c\uff0c\u5341\u5206\u5730\u4e0d\u53ef\u9760\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prototypal_%E5%8E%9F%E5%9E%8B%E5%BC%8F_%E7%B9%BC%E6%89%BF\"><\/span><strong>(Prototypal \u539f\u578b\u5f0f) \u7e7c\u627f<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function Foo(name) {\n    this.name = name;\n}\nFoo.prototype.myName = function() {\n    return this.name;\n};\nfunction Bar(name,label) {\n    Foo.call( this, name );\n    this.label = label;\n}\n\/\/ here, we make a new `Bar.prototype`\n\/\/ linked to `Foo.prototype`\nBar.prototype = Object.create( Foo.prototype );\n\/\/ Beware! Now `Bar.prototype.constructor` is gone,\n\/\/ and might need to be manually \"fixed\" if you're\n\/\/ in the habit of relying on such properties!\nBar.prototype.myLabel = function() {\n    return this.label;\n};\nvar a = new Bar( \"a\", \"obj a\" );\na.myName(); \/\/ \"a\"\na.myLabel(); \/\/ \"obj a\"<\/pre>\n\n\n<p>\u900f\u904e <code>Object.create(..)<\/code> \u5275\u5efa\u4e86\u4e00\u500b\u65b0\u7684\u7269\u4ef6\uff0c\u4e26\u5c07\u5176\u5167\u90e8\u7684 <code>[[Prototype]]<\/code> \u9023\u7d50\u5230\u6307\u5b9a\u7684\u7269\u4ef6\u4e0a\u3002<\/p>\n\n\n<p>\u5e38\u898b\u7684\u8aa4\u89e3\u662f\u63a1\u7528\u4ee5\u4e0b\u7684\u65b9\u5f0f\u4f86\u5efa\u7acb\uff0c\u4f46\u6703\u5e36\u6709\u4e00\u4e9b\u526f\u4f5c\u7528\uff1a<\/p>\n\n\n<p>(1) <code>Bar.prototype = Foo.prototype;<\/code><br \/>\u53ea\u662f\u5efa\u7acb\u4e00\u500b\u5171\u7528\u53c3\u8003\uff0c\u7576\u4fee\u6539 <code>Bar.prototype.myLabel = ...<\/code> \u6642\uff0c\u6703\u5f71\u97ff\u5230\u53c3\u8003\u4efb\u4f55\u9023\u7d50\u5230 <code>Foo.prototype<\/code> \u7684\u7269\u4ef6\u3002<\/p>\n\n\n<p>(2) <code>Bar.prototype = new Foo();<\/code><br \/>\u7684\u78ba\u6703\u5efa\u7acb\u65b0\u7684\u7269\u4ef6\u4e26\u4e14\u6b63\u78ba\u9023\u7d50\uff0c\u4f46\u5982\u679c\u51fd\u5f0f\u6709\u6539\u8b8a\u72c0\u614b\u3001\u65b0\u589e\u8cc7\u6599\u7279\u6027\u7d66 <code>this<\/code> \u7b49\u7b49\u526f\u4f5c\u7528\u884c\u70ba\u767c\u751f\u6642\uff0c\u5f88\u53ef\u80fd\u5c0d\u932f\u7684\u7269\u4ef6\u505a\u9019\u4e9b\u884c\u70ba\u3002<\/p>\n\n\n<p>ES6 \u4e4b\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528<code> Object.setPrototypeof(..)<\/code> \u4f86\u4fee\u6539 <code>Bar.prototype<\/code>\u3002<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ pre-ES6\n\/\/ throws away default existing `Bar.prototype`\nBar.prototype = Object.create( Foo.prototype );\n\/\/ ES6+\n\/\/ modifies existing `Bar.prototype`\nObject.setPrototypeOf( Bar.prototype, Foo.prototype );<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%AA%A2%E8%A6%96%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D%E9%97%9C%E4%BF%82\"><\/span>\u6aa2\u8996\u300c\u985e\u5225\u300d\u95dc\u4fc2<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u6aa2\u8996\u7269\u4ef6\u7684\u7e7c\u627f\u4e16\u7cfb (inheritance ancestry)\uff0c\u5728\u50b3\u7d71\u985e\u5225\u5c0e\u5411\u4e2d\uff0c\u88ab\u7a31\u70ba introspection (\u5167\u7701)\u3002<\/p>\n\n\n<p><code>instanceof<\/code> \u904b\u7b97\u5b50\uff1a\u7528\u6cd5\u70ba <code>[Object] instanceof [Function]<\/code>\uff0c\u9019\u4f7f\u5f97\u53ea\u6709\u5728\u6709\u51fd\u5f0f\u53ef\u4ee5\u6e2c\u8a66\u7684\u60c5\u6cc1\u4e0b\uff0c\u624d\u53ef\u4ee5\u67e5\u8a62\u67d0\u500b\u7269\u4ef6\u7684\u4e16\u7cfb\u3002\u82e5\u60f3\u77e5\u9053 a\u3001b \u5169\u500b\u7269\u4ef6\u662f\u5426\u6709\u4e32\u93c8\uff0c\u4f7f\u7528\u6b64\u904b\u7b97\u5b50\u5c31\u5e6b\u4e0d\u4e0a\u5fd9\u4e86\u3002<\/p>\n\n\n<p>\u9032\u884c\u53cd\u601d\u8f03\u70ba\u5408\u9069\u7684\u3001\u4e7e\u6de8\u7684\u4f5c\u6cd5\uff0c\u662f\u63a1\u7528 <code>isPrototypeOf(..)<\/code>\uff0c\u5982 <code>b.isPrototypeOf(c)<\/code>\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%89%A9%E4%BB%B6%E9%80%A3%E7%B5%90\"><\/span><strong>\u7269\u4ef6\u9023\u7d50<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u4f7f\u7528 <code>Object.create(..)<\/code> \u5efa\u7acb\u7269\u4ef6\u7684\u9023\u7d50\uff0c\u662f\u6bd4\u8f03\u597d\u7684\u65b9\u5f0f\uff0c\u5e6b\u52a9\u6211\u5011\u5efa\u7acb\u65b0\u7684\u7269\u4ef6\uff0c\u4e26\u9023\u7d50\u5230\u6307\u5b9a\u7684\u7269\u4ef6\u4e0a\u3002\u800c\u6c92\u6709 new, .prototype, .constructor \u7b49\u7b49\u4ee4\u4eba\u56f0\u60d1\u7684\u53c3\u8003\u3002<\/p>\n\n\n<p>\u5728 ES5 \u4e4b\u524d\u7684\u74b0\u5883\u4f7f\u7528\u7684\u8a71\uff0c\u9700\u8981 polyfill\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%80%A3%E7%B5%90%E4%BD%9C%E7%82%BA%E5%82%99%E6%8F%B4%E4%B9%8B%E7%94%A8%EF%BC%9F\"><\/span>\u9023\u7d50\u4f5c\u70ba\u5099\u63f4\u4e4b\u7528\uff1f<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u4e0d\u8981\u5c07\u7269\u4ef6\u9023\u7d50\u4ee5\u4f5c\u70ba\u5099\u63f4\uff0c\u8003\u616e\uff1a<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var anotherObject = {\n    cool: function() {\n        console.log( \"cool!\" );\n    }\n};\nvar myObject = Object.create( anotherObject );\nmyObject.cool(); \/\/ \"cool!\"<\/pre>\n\n\n<p>\u8a2d\u8a08\u51fa\u8b93\u958b\u767c\u4eba\u54e1\u5728 myObject \u6c92\u6709 cool \u65b9\u6cd5\u7684\u60c5\u6cc1\u4e0b\u4f7f\u7528 <code>myObject.cool()<\/code>\uff0c\u610f\u5473\u8457\u7a0b\u5f0f\u4e2d\u591a\u4e86\u4e00\u4e9b\u4ee4\u4eba\u9a5a\u8a1d\u548c\u4e0d\u53ef\u9810\u671f\u7684\u90e8\u5206\uff0c\u8acb\u907f\u514d\u4f7f\u7528\u9019\u6a23\u7684\u65b9\u5f0f\uff0c\u6216\u662f\u4ee5\u59d4\u6d3e (delegation\uff0c\u53c3\u95b1\u7b2c 11 \u7ae0) \u7684\u4f5c\u6cd5\u4f86\u5be6\u73fe\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A4%87%E7%BF%92\"><\/span><strong>\u8907\u7fd2<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<ul class=\"wp-block-list\"><li>\u539f\u578b\u4e32\u93c8 (prototype chain) \u610f\u6307\u5c0b\u8a2a\u7269\u4ef6\u7684\u4e32\u93c8\u4f86\u5c0b\u627e\u67d0\u500b\u7279\u6027\u3002<\/li><li>\u6240\u6709\u6b63\u5e38\u7269\u4ef6\u7684\u9802\u7aef\u90fd\u662f <code>Object.prototype<\/code>\u3002<\/li><li>\u9019\u4e9b\u6a5f\u5236\u770b\u8d77\u4f86\u5f88\u985e\u4f3c\u985e\u5225\u5c0e\u5411\u8a9e\u8a00\u4e2d\u7684\u5be6\u9ad4\u5316\u6216\u7e7c\u627f\uff0c\u4f46\u662f\u5be6\u969b\u4e0a\u5728 JS \u4e2d\uff0c\u4e26\u4e0d\u6703\u9032\u884c\u62f7\u8c9d\u7684\u52d5\u4f5c\uff0c\u53ea\u662f\u7528\u7269\u4ef6\u4e32\u806f\u5f7c\u6b64\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>[[Prototype]] JS \u7684\u7269\u4ef6\u4e2d\u6709\u4e00\u500b &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[13,12],"tags":[21],"class_list":["post-415","post","type-post","status-publish","format-standard","hentry","category-jsclosuresthis","category-12","tag-javascript"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=415"}],"version-history":[{"count":0,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}