{"id":365,"date":"2023-10-27T14:31:47","date_gmt":"2023-10-27T06:31:47","guid":{"rendered":"https:\/\/www.qzdd.net\/?p=365"},"modified":"2023-10-27T14:31:47","modified_gmt":"2023-10-27T06:31:47","slug":"prototype%e5%8e%9f%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.qzdd.net\/?p=365","title":{"rendered":"Prototype\u539f\u578b"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>const fn = function(){};  <em>\/\/ \u81ea\u52a8\u5e26prototype\u5c5e\u6027\uff0c\u662f\u4e00\u4e2a\u5177\u6709constructor\uff08\u6307\u5411\u81ea\u8eab\uff09\u5c5e\u6027\u7684\u5bf9\u8c61<\/em>\nfn.__proto__ === Function.prototype;  \/\/ true\nfn.prototype !== fn.__proto__;  \/\/ true\nfn.prototype !== Funtion.prototype;  \/\/ true\nfn.prototype.constructor === fn;  \/\/ true\nnew fn().constructor === fn; \/\/ true\nfn.constructor === Function;  \/\/ true <em>\uff08\u5b9e\u4f8b\u7684constructor\u6307\u5411\u5b9e\u4f8b\u5bf9\u5e94\u7684\u7c7b\uff09<\/em>\n\nclass Space {\n    constructor(){}\n}\nSpace.__proto__ === Function.prototype;  \/\/ true\nSpace.prototype !== Space.__proto__;  \/\/ true\nSpace.prototype !== Funtion.prototype;  \/\/ true\nSpace.prototype.constructor === Space;  \/\/ true , <em>\u7c7b\u81ea\u52a8\u5e26prototype\u5c5e\u6027\uff0c\u662f\u4e00\u4e2a\u5177\u6709constructor\uff08\u6307\u5411\u81ea\u8eab\uff09\u5c5e\u6027\u7684\u5bf9\u8c61<\/em>\nnew Space().constructor === Space;  \/\/ true\nSpace.constructor === Function;  \/\/ true \nconst obj = {};\nobj.constructor === Object;  \/\/ true\n\/\/ prototype\u539f\u578b\u5bf9\u8c61\u7684constructor\u6307\u5411\u7c7b\u672c\u8eab\uff0c\u56e0\u6b64\n\/\/ \u5b9e\u4f8b\uff08\u5bf9\u8c61\uff09\u7ee7\u627fconstructor\u5c5e\u6027\uff0c\u4e5f\u6307\u5411\u5bf9\u5e94\u7684\u7c7b\u672c\u8eab\uff0c\u5b9e\u4f8b\u5177\u6709__proto__\u8bbf\u95ee\u5668\u5c5e\u6027\uff08\u5f03\u7528\u4e0d\u63a8\u8350\uff09\u6307\u5411\u7c7b\uff08\u7ed1\u5b9a\u7684\uff09\u539f\u578b\n\/\/ <strong>\u7c7b\u540d.constructor\u548c\u7c7b\u5b9a\u4e49\u4e2d\u7684constructor\u4e0d\u662f\u4e00\u56de\u4e8b<\/strong>\uff08<strong><em>\u7c7b\u662f\u6784\u9020\u51fd\u6570\u8bed\u6cd5\u7cd6<\/em><\/strong>\uff09\n\/\/ \u7c7b\u540d(\u51fd\u6570\u540d).prototype\u662f\u6307\u7c7b\u6216\u51fd\u6570\u88ab\u4f5c\u4e3a\u6784\u9020\u5668\u65f6\u5206\u914d\u7ed9\u5176\u5b9e\u4f8b\u5bf9\u8c61\u7684\u539f\u578b\uff0c\u7c7b\u540d.__proto__(\u6216 &#91;&#91;Prototype]] )\u662f\u6307\u4f5c\u4e3a\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u672c\u8eab\u7684\u539f\u578b<\/code><\/pre>\n\n\n\n<p>\u5927\u8bdd\u539f\u578b\u94fe<\/p>\n\n\n\n<p>prototype\u5982\u540c\u4e08\u592b\uff0cconstructor\u5982\u540c\u59bb\u5b50\uff08\u6216\u6bcd\u4eb2\uff09\uff0c__proto__\u5982\u540c\u7236\u4eb2\u3002<br>\u6bcf\u4e2afunction\u58f0\u660e\u540e\uff0c\u4f1a\u81ea\u52a8\u7ed1\u5b9a\u4e00\u4e2aprototype\u7684\u7a7a\u4e08\u592b\uff08Object\u7684\u5b9e\u4f8b\uff09\uff0c\u7b49\u5230\u6210\u5bb6\u65f6\u4f1a\u4fee\u6539prototype\u6307\u5411\u771f\u6b63\u7684\u4e08\u592b\u3002\u6bcf\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u672a\u6210\u5bb6\u65f6\uff0cconstructor\u6307\u5411\u6bcd\u4eb2\uff0c\u7b49\u5230\u6210\u5bb6\u540econstructor\u6307\u5411\u59bb\u5b50\uff0c__proto__\u4e00\u76f4\u6307\u5411\u7236\u4eb2\u3002Object\u5c31\u50cf\u5973\u5a32\uff0cObject.prototype\u5c31\u50cf\u4f0f\u7fb2\u3002<\/p>\n\n\n\n<p>class A extends B \uff0cBA\u5c31\u50cf\u662f\u5a46\u5ab3\u5173\u7cfb\u3002A\u7684\u4e08\u592b(prototype)\u662fB\u7684\u513f\u5b50\uff0c\u4e08\u592b(B\u7684\u513f\u5b50)\u7684constructor\u6307\u5411\u59bb\u5b50A\uff0c\u4e08\u592b\u7684__proto__\u6307\u5411\u5176\u7236\u4eb2(B\u7684\u4e08\u592b-B.prototype)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u8bdd\u539f\u578b\u94fe prototype\u5982\u540c\u4e08\u592b\uff0cconstructor\u5982\u540c\u59bb\u5b50\uff08\u6216\u6bcd\u4eb2\uff09\uff0c__proto__\u5982\u540c\u7236\u4eb2\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[53,72],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-base","tag-prototype","tag-72"],"_links":{"self":[{"href":"https:\/\/www.qzdd.net\/index.php?rest_route=\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qzdd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qzdd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qzdd.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qzdd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":0,"href":"https:\/\/www.qzdd.net\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.qzdd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qzdd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qzdd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}