{"id":166072,"date":"2024-05-08T15:29:11","date_gmt":"2024-05-08T07:29:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/166072.html"},"modified":"2024-05-08T15:29:14","modified_gmt":"2024-05-08T07:29:14","slug":"javascript%e4%b8%adprototype%e4%b8%8econstructors%e7%9a%84%e5%85%b3%e7%b3%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/166072.html","title":{"rendered":"javascript\u4e2dprototype\u4e0econstructors\u7684\u5173\u7cfb"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233147\/389ce5f0-469e-4d53-9862-d94f95f684f5.webp\" alt=\"javascript\u4e2dprototype\u4e0econstructors\u7684\u5173\u7cfb\" \/><\/p>\n<p><p>JavaScript \u4e2d\u7684 <strong>prototype<\/strong> \u4e0e <strong>constructors<\/strong> \u4e24\u8005\u4e4b\u95f4\u5b58\u5728\u5bc6\u4e0d\u53ef\u5206\u7684\u5173\u7cfb\u3002<strong>\u539f\u578b\uff08Prototype\uff09<\/strong> \u662f\u5bf9\u8c61\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u6a21\u677f\uff0c\u800c <strong>\u6784\u9020\u51fd\u6570\uff08Constructor\uff09<\/strong> \u5219\u662f\u7528\u6765\u521b\u5efa\u5177\u6709\u7279\u5b9a\u539f\u578b\u7684\u65b0\u5bf9\u8c61\u7684\u51fd\u6570\u3002<strong>\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u62e5\u6709\u4e00\u4e2a\u539f\u578b\u5bf9\u8c61<\/strong>\uff0c<strong>\u6240\u6709\u7531\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5bf9\u8c61\u5b9e\u4f8b\u90fd\u5c06\u5176\u539f\u578b\u6307\u5411\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u5bf9\u8c61<\/strong>\uff0c\u8fd9\u6837\u5b9e\u4f8b\u5c31\u53ef\u4ee5\u8bbf\u95ee\u539f\u578b\u4e2d\u5b9a\u4e49\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u539f\u578b\u5bf9\u8c61\u672c\u8eab\u5305\u542b\u4e00\u4e2a\u6307\u5411\u521b\u5efa\u5b83\u7684\u6784\u9020\u51fd\u6570\u7684\u6307\u9488\uff0c\u5373 <strong>constructor<\/strong> \u5c5e\u6027\uff0c\u5f62\u6210\u4e86\u4e00\u4e2a\u5faa\u73af\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e24\u8005\u7684\u5177\u4f53\u5173\u7cfb\u548c\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001CONSTRUCTORS\uff1a\u6784\u9020\u51fd\u6570\u7684\u4f5c\u7528<\/h3>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u5728JavaScript\u4e2d\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u3002\u5f53\u4f60\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u8be5\u51fd\u6570\u5c31\u4f1a\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u6765\u4f7f\u7528\u3002\u6784\u9020\u51fd\u6570\u901a\u5e38\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u4ee5\u4fbf\u8bed\u4e49\u4e0a\u4e0e\u5e38\u89c4\u51fd\u6570\u8fdb\u884c\u533a\u5206\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5bf9\u8c61\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u5f53\u65b0\u7684\u5bf9\u8c61\u901a\u8fc7\u6784\u9020\u51fd\u6570\u88ab\u521b\u5efa\u65f6\uff0c\u8be5\u5bf9\u8c61\u4f1a\u81ea\u52a8\u7ee7\u627f\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u3002\u521b\u5efa\u51fa\u7684\u5bf9\u8c61\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\uff0c\u6bd4\u5982Object\u3001Array\u6216\u8005\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Person(name, age) {<\/p>\n<p>  this.name = name;<\/p>\n<p>  this.age = age;<\/p>\n<p>}<\/p>\n<p>var person1 = new Person(&quot;Alice&quot;, 25);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Person<\/code> \u662f\u6784\u9020\u51fd\u6570\uff0c<code>person1<\/code> \u662f\u901a\u8fc7\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u6784\u9020\u51fd\u6570\u7684\u539f\u578b\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5c5e\u6027\u53eb\u505a prototype\u3002\u8fd9\u4e2a\u5c5e\u6027\u662f\u4e00\u4e2a\u6307\u5411\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u800c\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u662f\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u7684\u539f\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001PROTOTYPE\uff1a\u539f\u578b\u7684\u4f5c\u7528<\/h3>\n<\/p>\n<p><p>\u539f\u578b\u662f\u5bf9\u8c61\u7ee7\u627f\u529f\u80fd\u7684\u57fa\u7840\u3002\u5728JavaScript\u4e2d\uff0c\u539f\u578b\u662f\u4e00\u4e2a\u5141\u8bb8\u5bf9\u8c61\u5171\u4eab\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>\u539f\u578b\u7684\u7ee7\u627f\u673a\u5236<\/h4>\n<\/p>\n<p><p>\u539f\u578b\u7ee7\u627f\u662fJavaScript\u4e2d\u5b9e\u73b0\u5bf9\u8c61\u95f4\u7ee7\u627f\u7684\u4e00\u79cd\u673a\u5236\u3002\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u4e0e\u4e4b\u5173\u8054\u7684\u539f\u578b\u5bf9\u8c61\uff0c\u5e76\u4ece\u4e2d\u7ee7\u627f\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">Person.prototype.sayHello = function() {<\/p>\n<p>  console.log(&quot;Hello, my name is &quot; + this.name);<\/p>\n<p>};<\/p>\n<p>person1.sayHello(); \/\/ \u8f93\u51fa: Hello, my name is Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>sayHello<\/code> \u65b9\u6cd5\u88ab\u6dfb\u52a0\u5230 <code>Person<\/code> \u7684\u539f\u578b\u4e2d\uff0c\u6240\u6709 <code>Person<\/code> \u5b9e\u4f8b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u539f\u578b\u94fe<\/h4>\n<\/p>\n<p><p>\u5f53\u8bbf\u95ee\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u8fd9\u4e2a\u5bf9\u8c61\u672c\u8eab\u6ca1\u6709\u8fd9\u4e2a\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u89e3\u91ca\u5668\u4f1a\u5728\u539f\u578b\u94fe\u4e0a\u67e5\u627e\u8fd9\u4e2a\u5c5e\u6027\u6216\u65b9\u6cd5\u3002\u539f\u578b\u94fe\u7684\u6839\u662f Object.prototype\uff0c\u6240\u6709\u5bf9\u8c61\u7684\u65b9\u6cd5\u5982 <code>toString<\/code> \u90fd\u662f\u4ece\u8fd9\u91cc\u7ee7\u627f\u6765\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001CONSTRUCTORS AND PROTOTYPE\uff1a\u6784\u9020\u51fd\u6570\u4e0e\u539f\u578b\u7684\u76f8\u4e92\u5173\u7cfb<\/h3>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u548c\u539f\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u662f\u901a\u8fc7\u4e24\u4e2a\u5c5e\u6027\u4e92\u76f8\u8fde\u63a5\u7684\uff1a\u6784\u9020\u51fd\u6570\u7684 <strong>prototype<\/strong> \u5c5e\u6027\u548c\u6bcf\u4e2a\u539f\u578b\u5bf9\u8c61\u7684 <strong>constructor<\/strong> \u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u6784\u9020\u51fd\u6570\u7684 prototype \u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u90fd\u6709\u4e00\u4e2a <code>prototype<\/code> \u5c5e\u6027\uff0c\u6307\u5411\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u6b63\u662f\u8c03\u7528\u8be5\u6784\u9020\u51fd\u6570\u6240\u521b\u5efa\u7684\u5b9e\u4f8b\u7684\u539f\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">console.log(Person.prototype.constructor === Person); \/\/ \u8f93\u51fa: true<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u539f\u578b\u5bf9\u8c61\u7684 constructor \u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2a\u539f\u578b\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a <code>constructor<\/code> \u5c5e\u6027\uff0c\u6307\u5411\u5176\u76f8\u5173\u8054\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u4e3e\u4f8b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6784\u9020\u51fd\u6570\u548c\u539f\u578b\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u5bf9\u8c61\u7684\u7ee7\u627f\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5177\u6709\u5171\u4eab\u65b9\u6cd5\u7684\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">function Animal(name) {<\/p>\n<p>  this.name = name;<\/p>\n<p>}<\/p>\n<p>Animal.prototype.getName = function() {<\/p>\n<p>  return this.name;<\/p>\n<p>};<\/p>\n<p>var dog = new Animal(&quot;Buddy&quot;);<\/p>\n<p>console.log(dog.getName()); \/\/ \u8f93\u51fa: Buddy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6240\u6709 <code>Animal<\/code> \u7c7b\u578b\u7684\u5b9e\u4f8b\u90fd\u4f1a\u5171\u4eab <code>getName<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u539f\u578b\u4e0e\u52a8\u6001\u6027<\/h4>\n<\/p>\n<p><p>JavaScript\u7684\u539f\u578b\u673a\u5236\u662f\u52a8\u6001\u7684\uff0c\u5982\u679c\u4f60\u5411\u539f\u578b\u6dfb\u52a0\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\uff0c\u6240\u6709\u57fa\u4e8e\u8be5\u539f\u578b\u521b\u5efa\u7684\u5bf9\u8c61\u90fd\u4f1a\u7acb\u5373\u62e5\u6709\u8fd9\u4e9b\u6dfb\u52a0\u7684\u5c5e\u6027\u6216\u8005\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">Person.prototype.sayGoodbye = function() {<\/p>\n<p>  console.log(this.name + &quot; says goodbye.&quot;);<\/p>\n<p>};<\/p>\n<p>person1.sayGoodbye(); \/\/ \u8f93\u51fa: Alice says goodbye.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4fbf\u662f prototype \u548c constructors \u5728JavaScript\u4e2d\u7684\u76f8\u4e92\u5173\u7cfb\u548c\u5b83\u4eec\u7684\u5de5\u4f5c\u673a\u5236\u3002\u900f\u8fc7\u5bf9\u8fd9\u4e9b\u5173\u7cfb\u7684\u6df1\u5165\u7406\u89e3\uff0c\u53ef\u4ee5\u5141\u8bb8\u5f00\u53d1\u8005\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u548c\u66f4\u5177\u53ef\u7ef4\u62a4\u6027\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662fJavaScript\u4e2d\u7684prototype\u548cconstructors?<\/strong><\/p>\n<p>JavaScript\u4e2d\u7684prototype\u662f\u4e00\u4e2a\u539f\u578b\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u4e00\u4e2aprototype\u5c5e\u6027\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u6dfb\u52a0\u5171\u4eab\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7ed9\u8be5\u51fd\u6570\u7684\u5b9e\u4f8b\u3002\u800cconstructors\u5219\u662f\u6307\u51fd\u6570\u672c\u8eab\uff0c\u7528\u4e8e\u521b\u5efa\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n<p><strong>2. prototype\u548cconstructors\u5728JavaScript\u4e2d\u8d77\u5230\u4ec0\u4e48\u4f5c\u7528?<\/strong><\/p>\n<p>\u5728JavaScript\u4e2d\uff0c\u4f7f\u7528prototype\u548cconstructors\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u8c61\u4e4b\u95f4\u7684\u7ee7\u627f\u5173\u7cfb\u3002\u901a\u8fc7\u5728prototype\u4e2d\u6dfb\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5b9e\u4f8b\u5bf9\u8c61\u53ef\u4ee5\u5171\u4eab\u5b83\u4eec\uff0c\u800cconstructors\u5219\u7528\u4e8e\u521b\u5efa\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528prototype\u548cconstructors\u5728JavaScript\u4e2d\u5b9e\u73b0\u7ee7\u627f?<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u7ee7\u627f\uff0c\u53ef\u4ee5\u5728\u5b50\u7c7b\u7684prototype\u4e2d\u5c06\u7236\u7c7b\u7684\u5b9e\u4f8b\u8d4b\u503c\u7ed9\u5b83\uff0c\u7136\u540e\u518d\u5c06\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6307\u5411\u5b50\u7c7b\u81ea\u8eab\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u8ba9\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e76\u4e14\u5b9e\u4f8b\u5316\u5b50\u7c7b\u65f6\u53ef\u4ee5\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728JavaScript\u4e2d\u5b9e\u73b0\u57fa\u4e8e\u539f\u578b\u7684\u7ee7\u627f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u4e2d\u7684 prototype \u4e0e constructors \u4e24\u8005\u4e4b\u95f4\u5b58\u5728\u5bc6\u4e0d\u53ef\u5206\u7684\u5173\u7cfb\u3002\u539f\u578b [&hellip;]","protected":false},"author":3,"featured_media":166075,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166072"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=166072"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/166072\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/166075"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=166072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=166072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=166072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}