{"id":5113,"date":"2022-02-21T23:29:13","date_gmt":"2022-02-21T15:29:13","guid":{"rendered":"http:\/\/123.57.164.21\/?p=5113"},"modified":"2022-02-21T23:29:13","modified_gmt":"2022-02-21T15:29:13","slug":"js%e4%b8%ad%e7%9a%84%e5%8e%9f%e5%9e%8b%e5%92%8c%e5%8e%9f%e5%9e%8b%e9%93%be%ef%bc%88%e5%9b%be%e8%a7%a3%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=5113","title":{"rendered":"JS\u4e2d\u7684\u539f\u578b\u548c\u539f\u578b\u94fe\uff08\u56fe\u89e3\uff09"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1ahttps:\/\/blog.csdn.net\/qq_36996271\/article\/details\/82527256<\/p>\n\n\n\n<p>JS\u4e2d\u7684\u539f\u578b\u548c\u539f\u578b\u94fe<\/p>\n\n\n\n<p>\u8bb2\u539f\u578b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e94\u8be5\u5148\u8981\u8bb0\u4f4f\u4ee5\u4e0b\u51e0\u4e2a\u8981\u70b9\uff0c\u8fd9\u51e0\u4e2a\u8981\u70b9\u662f\u7406\u89e3\u539f\u578b\u7684\u5173\u952e\uff1a<\/p>\n\n\n\n<p>1\u3001\u6240\u6709\u7684\u5f15\u7528\u7c7b\u578b\uff08\u6570\u7ec4\u3001\u51fd\u6570\u3001\u5bf9\u8c61\uff09\u53ef\u4ee5\u81ea\u7531\u6269\u5c55\u5c5e\u6027\uff08\u9664null\u4ee5\u5916\uff09\u3002<\/p>\n\n\n\n<p>2\u3001\u6240\u6709\u7684\u5f15\u7528\u7c7b\u578b\u90fd\u6709\u4e00\u4e2a\u2019_ _ proto_ _&#8217;\u5c5e\u6027(\u4e5f\u53eb\u9690\u5f0f\u539f\u578b\uff0c\u5b83\u662f\u4e00\u4e2a\u666e\u901a\u7684\u5bf9\u8c61)\u3002<\/p>\n\n\n\n<p>3\u3001\u6240\u6709\u7684\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u2019prototype\u2019\u5c5e\u6027(\u8fd9\u4e5f\u53eb\u663e\u5f0f\u539f\u578b\uff0c\u5b83\u4e5f\u662f\u4e00\u4e2a\u666e\u901a\u7684\u5bf9\u8c61)\u3002<\/p>\n\n\n\n<p>4\u3001\u6240\u6709\u5f15\u7528\u7c7b\u578b\uff0c\u5b83\u7684\u2019_ _ proto_ _&#8217;\u5c5e\u6027\u6307\u5411\u5b83\u7684\u6784\u9020\u51fd\u6570\u7684\u2019prototype\u2019\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>5\u3001\u5f53\u8bd5\u56fe\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\uff0c\u5982\u679c\u8fd9\u4e2a\u5bf9\u8c61\u672c\u8eab\u4e0d\u5b58\u5728\u8fd9\u4e2a\u5c5e\u6027\uff0c\u90a3\u4e48\u5c31\u4f1a\u53bb\u5b83\u7684\u2019_ _ proto_ _&#8217;\u5c5e\u6027(\u4e5f\u5c31\u662f\u5b83\u7684\u6784\u9020\u51fd\u6570\u7684\u2019prototype\u2019\u5c5e\u6027)\u4e2d\u53bb\u5bfb\u627e\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u8981\u70b9\u8bf4\u5b8c\u4e86\uff0c\u6211\u4eec\u5c31\u6839\u636e\u8fd9\u4e9b\u8981\u70b9\u6765\u7406\u89e3\u539f\u578b\u548c\u539f\u578b\u94fe\u3002<\/p>\n\n\n\n<p><strong>\u539f\u578b<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a<strong>\u539f\u578b<\/strong>\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/\u8fd9\u662f\u4e00\u4e2a\u6784\u9020\u51fd\u6570\n    function Foo(name,age){\n        this.name=name;\n        this.age=age;\n    }\n    \/*\u6839\u636e\u8981\u70b93\uff0c\u6240\u6709\u7684\u51fd\u6570\u90fd\u6709\u4e00\u4e2aprototype\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u662f\u4e00\u4e2a\u5bf9\u8c61\n    \u518d\u6839\u636e\u8981\u70b91\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u53ef\u4ee5\u81ea\u7531\u6269\u5c55\u5c5e\u6027\n    \u4e8e\u662f\u5c31\u6709\u4e86\u4ee5\u4e0b\u5199\u6cd5*\/\n    Foo.prototype={\n        \/\/ prototype\u5bf9\u8c61\u91cc\u9762\u53c8\u6709\u5176\u4ed6\u7684\u5c5e\u6027\n        showName:function(){\n            console.log(\"I'm \"+this.name);\/\/this\u662f\u4ec0\u4e48\u8981\u770b\u6267\u884c\u7684\u65f6\u5019\u8c01\u8c03\u7528\u4e86\u8fd9\u4e2a\u51fd\u6570\n        },\n        showAge:function(){\n            console.log(\"And I'm \"+this.age);\/\/this\u662f\u4ec0\u4e48\u8981\u770b\u6267\u884c\u7684\u65f6\u5019\u8c01\u8c03\u7528\u4e86\u8fd9\u4e2a\u51fd\u6570\n        }\n    }\n    var fn=new Foo('\u5c0f\u660e',19)\n    \/*\u5f53\u8bd5\u56fe\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\uff0c\u5982\u679c\u8fd9\u4e2a\u5bf9\u8c61\u672c\u8eab\u4e0d\u5b58\u5728\u8fd9\u4e2a\u5c5e\u6027\uff0c\u90a3\u4e48\u5c31\u4f1a\u53bb\u5b83\n    \u6784\u9020\u51fd\u6570\u7684'prototype'\u5c5e\u6027\u4e2d\u53bb\u627e*\/\n    fn.showName(); \/\/I'm \u5c0f\u660e\n    fn.showAge(); \/\/And I'm 19<\/pre>\n\n\n\n<p>\u8fd9\u5c31\u662f\u539f\u578b\uff0c\u5f88\u597d\u7406\u89e3\u3002\u90a3\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u539f\u578b\u5462\uff1f<\/p>\n\n\n\n<p>\u8bd5\u60f3\u5982\u679c\u6211\u4eec\u8981\u901a\u8fc7Foo()\u6765\u521b\u5efa<strong>\u5f88\u591a\u5f88\u591a\u4e2a<\/strong>\u5bf9\u8c61\uff0c\u5982\u679c\u6211\u4eec\u662f\u8fd9\u6837\u5b50\u5199\u7684\u8bdd\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\tfunction Foo(name,age){\n\t\t\tthis.name=name;\n\t\t\tthis.age=age;\n\t\t\tthis.showName=function(){\n\t\t\t\tconsole.log(\"I'm \"+this.name);\n\t\t\t}\n\t\t\tthis.showAge=function(){\n\t\t\t\tconsole.log(\"And I'm \"+this.age);\n\t\t\t}\n\t\t}\n<\/pre>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u521b\u5efa\u51fa\u6765\u7684\u6bcf\u4e00\u4e2a\u5bf9\u8c61\uff0c\u91cc\u9762\u90fd\u6709showName\u548cshowAge\u65b9\u6cd5\uff0c\u8fd9\u6837\u5c31\u4f1a\u5360\u7528\u5f88\u591a\u7684\u8d44\u6e90\u3002<br>\u800c\u901a\u8fc7\u539f\u578b\u6765\u5b9e\u73b0\u7684\u8bdd\uff0c\u53ea\u9700\u8981\u5728\u6784\u9020\u51fd\u6570\u91cc\u9762\u7ed9\u5c5e\u6027\u8d4b\u503c\uff0c\u800c\u628a\u65b9\u6cd5\u5199\u5728Foo.prototype\u5c5e\u6027(\u8fd9\u4e2a\u5c5e\u6027\u662f\u552f\u4e00\u7684)\u91cc\u9762\u3002\u8fd9\u6837\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u53ef\u4ee5\u4f7f\u7528prototype\u5c5e\u6027\u91cc\u9762\u7684showName\u3001showAge\u65b9\u6cd5\uff0c\u5e76\u4e14\u8282\u7701\u4e86\u4e0d\u5c11\u7684\u8d44\u6e90\u3002<br><\/p>\n\n\n\n<p><strong>\u539f\u578b\u94fe<\/strong><\/p>\n\n\n\n<p>\u7406\u89e3\u4e86\u539f\u578b\uff0c\u90a3\u4e48\u539f\u578b\u94fe\u5c31\u66f4\u597d\u7406\u89e3\u4e86\u3002<br>\u4e0b\u9762\u8fd9\u6bb5\u8bdd\u53ef\u4ee5\u5e2e\u52a9\u7406\u89e3\u539f\u578b\u94fe<\/p>\n\n\n\n<p>\u6839\u636e\u8981\u70b95\uff0c\u5f53\u8bd5\u56fe\u5f97\u5230\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u65f6\uff0c\u5982\u679c\u8fd9\u4e2a\u5bf9\u8c61\u672c\u8eab\u4e0d\u5b58\u5728\u8fd9\u4e2a\u5c5e\u6027\uff0c\u90a3\u4e48\u5c31\u4f1a\u53bb\u5b83\u6784\u9020\u51fd\u6570\u7684\u2019prototype\u2019\u5c5e\u6027\u4e2d\u53bb\u5bfb\u627e\u3002\u90a3\u53c8\u56e0\u4e3a\u2019prototype\u2019\u5c5e\u6027\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5b83\u4e5f\u6709\u4e00\u4e2a\u2019_ _ proto_ _&#8217;\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\t\t\/\/ \u6784\u9020\u51fd\u6570\n\t\tfunction Foo(name,age){\n\t\t \tthis.name=name;\n\t\t \tthis.age=age;\n\t\t}\n\t\tObject.prototype.toString=function(){\n\t\t\t\/\/this\u662f\u4ec0\u4e48\u8981\u770b\u6267\u884c\u7684\u65f6\u5019\u8c01\u8c03\u7528\u4e86\u8fd9\u4e2a\u51fd\u6570\u3002\n\t\t\tconsole.log(\"I'm \"+this.name+\" And I'm \"+this.age);\n\t\t}\n\t\tvar fn=new Foo('\u5c0f\u660e',19);\n\t\tfn.toString(); \/\/I'm \u5c0f\u660e And I'm 19\n\t\tconsole.log(fn.toString===Foo.prototype.__proto__.toString); \/\/true\n\t\t\n\t\tconsole.log(fn.__proto__ ===Foo.prototype)\/\/true\n\t\tconsole.log(Foo.prototype.__proto__===Object.prototype)\/\/true\n\t\tconsole.log(Object.prototype.__proto__===null)\/\/true\n<\/pre>\n\n\n\n<p><br>\u662f\u4e0d\u662f\u89c9\u5f97\u6709\u70b9\u5947\u602a\uff1f\u6211\u4eec\u6765\u5206\u6790\u4e00\u4e0b\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/02\/\u56fe\u7247-52-1024x618.png\" alt=\"\" class=\"wp-image-5114\" width=\"466\" height=\"280\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/02\/\u56fe\u7247-52-1024x618.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/02\/\u56fe\u7247-52-300x181.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/02\/\u56fe\u7247-52-830x501.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/02\/\u56fe\u7247-52-230x139.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/02\/\u56fe\u7247-52-350x211.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/02\/\u56fe\u7247-52.png 1110w\" sizes=\"(max-width: 466px) 100vw, 466px\" \/><\/figure><\/div>\n\n\n\n<p><br><\/p>\n\n\n\n<p>\u9996\u5148\uff0cfn\u7684\u6784\u9020\u51fd\u6570\u662fFoo()\u3002\u6240\u4ee5\uff1a<br>fn._ _ proto _ <em>=== Foo.prototype \u53c8\u56e0\u4e3aFoo.prototype\u662f\u4e00\u4e2a\u666e\u901a\u7684\u5bf9\u8c61\uff0c\u5b83\u7684\u6784\u9020\u51fd\u6570\u662fObject\uff0c\u6240\u4ee5\uff1a Foo.prototype.<\/em> _ proto _ _=== Object.prototype<br>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u77e5\u9053\u8fd9\u4e2atoString()\u65b9\u6cd5\u662f\u5728Object.prototype\u91cc\u9762\u7684\uff0c\u5f53\u8c03\u7528\u8fd9\u4e2a\u5bf9\u8c61\u7684\u672c\u8eab\u5e76\u4e0d\u5b58\u5728\u7684\u65b9\u6cd5\u65f6\uff0c\u5b83\u4f1a\u4e00\u5c42\u4e00\u5c42\u5730\u5f80\u4e0a\u53bb\u627e\uff0c\u4e00\u76f4\u5230null\u4e3a\u6b62\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u5f53fn\u8c03\u7528toString()\u65f6\uff0cJS\u53d1\u73b0fn\u4e2d\u6ca1\u6709\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u4e8e\u662f\u5b83\u5c31\u53bbFoo.prototype\u4e2d\u53bb\u627e\uff0c\u53d1\u73b0\u8fd8\u662f\u6ca1\u6709\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u7136\u540e\u5c31\u53bbObject.prototype\u4e2d\u53bb\u627e\uff0c\u627e\u5230\u4e86\uff0c\u5c31\u8c03\u7528Object.prototype\u4e2d\u7684toString()\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u539f\u578b\u94fe\uff0cfn\u80fd\u591f\u8c03\u7528Object.prototype\u4e2d\u7684\u65b9\u6cd5\u6b63\u662f\u56e0\u4e3a\u5b58\u5728\u539f\u578b\u94fe\u7684\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u5728\u4f7f\u7528\u539f\u578b\u7684\u65f6\u5019\uff0c\u4e00\u822c\u63a8\u8350\u5c06\u9700\u8981\u6269\u5c55\u7684\u65b9\u6cd5\u5199\u5728\u6784\u9020\u51fd\u6570\u7684prototype\u5c5e\u6027\u4e2d\uff0c\u907f\u514d\u5199\u5728_ _ proto _ _\u5c5e\u6027\u91cc\u9762\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/blog.csdn.net\/qq_36996271\/article\/details\/82 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5113"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5113"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5113\/revisions"}],"predecessor-version":[{"id":5115,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/5113\/revisions\/5115"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}