{"id":170123,"date":"2024-05-08T17:01:08","date_gmt":"2024-05-08T09:01:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170123.html"},"modified":"2024-05-08T17:01:14","modified_gmt":"2024-05-08T09:01:14","slug":"javascript-%e5%88%9b%e5%bb%ba%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e7%9a%84%e6%96%b9%e6%b3%95%e6%9c%89%e5%93%aa%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/170123.html","title":{"rendered":"javascript \u521b\u5efa\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27040802\/7f87d418-dfb7-4144-a010-437e77621b6f.webp\" alt=\"javascript \u521b\u5efa\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b\" \/><\/p>\n<p><p><strong>JavaScript \u521b\u5efa\u6784\u9020\u51fd\u6570\u4e3b\u8981\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u901a\u8fc7\u51fd\u6570\u58f0\u660e\u5f62\u5f0f\u3001\u901a\u8fc7\u51fd\u6570\u8868\u8fbe\u5f0f\u5f62\u5f0f\u3001\u4ee5\u53ca\u4f7f\u7528 ES6 \u5f15\u5165\u7684 class \u5173\u952e\u5b57<\/strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528 class \u5173\u952e\u5b57\u662f\u6700\u8fd1\u5f15\u5165\u7684\u3001\u66f4\u52a0\u73b0\u4ee3\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u6e05\u6670\u548c\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5bf9\u8c61\u548c\u5b9e\u73b0\u7ee7\u627f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7\u51fd\u6570\u58f0\u660e\u5f62\u5f0f\u521b\u5efa\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u51fd\u6570\u58f0\u660e\u5f62\u5f0f\u521b\u5efa\u6784\u9020\u51fd\u6570\u662fJavaScript\u4e2d\u8f83\u4e3a\u4f20\u7edf\u7684\u65b9\u5f0f<\/strong>\u3002\u5728\u8fd9\u79cd\u65b9\u5f0f\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u5b9e\u8d28\u4e0a\u662f\u4e00\u4e2a\u666e\u901a\u51fd\u6570\uff0c\u4f46\u6309\u7167\u7ea6\u5b9a\uff0c\u5176\u540d\u79f0\u7684\u9996\u5b57\u6bcd\u901a\u5e38\u5927\u5199\u3002\u901a\u8fc7\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u7ee7\u627f\u81ea\u51fd\u6570\u7684\u539f\u578b\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>const john = new Person(&quot;John&quot;, 30);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u5355\u6027\u548c\u76f4\u89c2\u6027\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u64cd\u4f5c\u6784\u9020\u51fd\u6570\u5185\u90e8\u7684<code>this<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f7f\u5bf9\u8c61\u7684\u521b\u5efa\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u51fd\u6570\u8868\u8fbe\u5f0f\u5f62\u5f0f\u521b\u5efa\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u8868\u8fbe\u5f0f\u5141\u8bb8\u60a8\u5c06\u4e00\u4e2a\u533f\u540d\u7684\u6216\u547d\u540d\u7684\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002<strong>\u8fd9\u79cd\u65b9\u5f0f\u521b\u5efa\u6784\u9020\u51fd\u6570\u4e0e\u901a\u8fc7\u51fd\u6570\u58f0\u660e\u5f62\u5f0f\u76f8\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u7684\u7f16\u7801\u65b9\u5f0f<\/strong>\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u53d8\u91cf\u63d0\u5347\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const Person = function(name, age) {<\/p>\n<p>    this.name = name;<\/p>\n<p>    this.age = age;<\/p>\n<p>};<\/p>\n<p>const alice = new Person(&quot;Alice&quot;, 25);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5229\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\uff0c\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u53d8\u91cf\u4f20\u9012\uff0c\u6216\u8005\u5728\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u3002\u8fd9\u63d0\u4f9b\u4e86\u9ad8\u5ea6\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528ES6\u7684class\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p><strong>ES6\u5f15\u5165\u4e86class\u5173\u952e\u5b57\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u63a5\u8fd1\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u6784\u9020\u51fd\u6570\u548c\u5b9e\u73b0\u7ee7\u627f<\/strong>\u3002class\u8bed\u6cd5\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u6613\u8bfb\u3001\u6613\u5199\uff0c\u800c\u4e14\u4f7f\u6784\u9020\u51fd\u6570\u7684\u521b\u5efa\u548c\u7ee7\u627f\u53d8\u5f97\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">class Person {<\/p>\n<p>    constructor(name, age) {<\/p>\n<p>        this.name = name;<\/p>\n<p>        this.age = age;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>const bob = new Person(&quot;Bob&quot;, 28);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528class\u5173\u952e\u5b57\uff0c\u60a8\u53ef\u4ee5\u6e05\u6670\u5730\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0c\u5e76\u901a\u8fc7<code>constructor<\/code>\u65b9\u6cd5\u521d\u59cb\u5316\u5bf9\u8c61\u5c5e\u6027\u3002\u6b64\u5916\uff0cclass\u8bed\u6cd5\u8fd8\u652f\u6301\u9759\u6001\u65b9\u6cd5\u3001\u7ee7\u627f\uff08\u901a\u8fc7<code>extends<\/code>\u5173\u952e\u5b57\uff09\uff0c\u4ee5\u53ca\u57fa\u4e8e\u539f\u578b\u7684\u65b9\u6cd5\u5b9a\u4e49\uff0c\u8fd9\u4e9b\u7279\u6027\u4f7f\u5f97JavaScript\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u66f4\u52a0\u5f3a\u5927\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u6765\u521b\u5efa\u6784\u9020\u51fd\u6570\uff0c\u91cd\u8981\u7684\u662f\u7406\u89e3\u5176\u5e95\u5c42\u539f\u7406\u548c\u9002\u7528\u573a\u666f\u3002\u901a\u8fc7\u51fd\u6570\u58f0\u660e\u548c\u51fd\u6570\u8868\u8fbe\u5f0f\u521b\u5efa\u7684\u6784\u9020\u51fd\u6570\u5b9e\u8d28\u4e0a\u662f\u76f8\u540c\u7684\uff0c\u4e3b\u8981\u5dee\u5f02\u5728\u4e8e\u51fd\u6570\u63d0\u5347\u548c\u4ee3\u7801\u7ec4\u7ec7\u98ce\u683c\u3002\u800cclass\u5173\u952e\u5b57\u5219\u662f\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u73b0\u4ee3\u7684\u8bed\u6cd5\u7cd6\uff0c\u80cc\u540e\u4ecd\u7136\u4f7f\u7528\u7740JavaScript\u7684\u539f\u578b\u94fe\u3002\u7406\u89e3\u8fd9\u4e9b\u6982\u5ff5\u5bf9\u4e8e\u6df1\u5165\u638c\u63e1JavaScript\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662fJavaScript\u6784\u9020\u51fd\u6570\uff1f<\/strong><br \/>JavaScript\u6784\u9020\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u548c\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528\u6784\u9020\u51fd\u6570\uff0c\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u4ee3\u7801\u5e76\u521b\u5efa\u591a\u4e2a\u76f8\u4f3c\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528JavaScript\u521b\u5efa\u6784\u9020\u51fd\u6570\uff1f<\/strong><br \/>\u4f7f\u7528JavaScript\u521b\u5efa\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u3002\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u51fd\u6570\u58f0\u660e\u6216\u51fd\u6570\u8868\u8fbe\u5f0f\u6765\u5b9a\u4e49\u6784\u9020\u51fd\u6570\uff0c\u5e76\u4f7f\u7528<code>new<\/code>\u5173\u952e\u5b57\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u4f7f\u7528<code>this<\/code>\u5173\u952e\u5b57\u6765\u5f15\u7528\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u5e76\u4e3a\u8be5\u5bf9\u8c61\u6dfb\u52a0\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">function Person(name, age) {\n  this.name = name;\n  this.age = age;\n  this.greet = function() {\n    console.log(&quot;Hello, my name is &quot; + this.name + &quot; and I am &quot; + this.age + &quot; years old&quot;);\n  }\n}\n\nvar person1 = new Person(&quot;John&quot;, 25);\nvar person2 = new Person(&quot;Jane&quot;, 30);\n<\/code><\/pre>\n<p><strong>3. \u662f\u5426\u53ea\u80fd\u4f7f\u7528\u51fd\u6570\u6765\u521b\u5efaJavaScript\u6784\u9020\u51fd\u6570\uff1f<\/strong><br \/>\u867d\u7136\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u521b\u5efaJavaScript\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\uff0c\u4f46\u8fd8\u6709\u5176\u4ed6\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u529f\u80fd\u7684\u65b9\u6cd5\uff0c\u5982\u4f7f\u7528<code>class<\/code>\u5173\u952e\u5b57\u4eceES6\u5f00\u59cb\u5f15\u5165\u7684\u7c7b\u3002\u4f7f\u7528\u7c7b\u6765\u521b\u5efa\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u66f4\u52a0\u6e05\u6670\u548c\u9762\u5411\u5bf9\u8c61\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">class Person {\n  constructor(name, age) {\n    this.name = name;\n    this.age = age;\n  }\n\n  greet() {\n    console.log(&quot;Hello, my name is &quot; + this.name + &quot; and I am &quot; + this.age + &quot; years old&quot;);\n  }\n}\n\nvar person1 = new Person(&quot;John&quot;, 25);\nvar person2 = new Person(&quot;Jane&quot;, 30);\n<\/code><\/pre>\n<p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u521b\u5efa\u6784\u9020\u51fd\u6570\uff0c\u5176\u76ee\u7684\u90fd\u662f\u76f8\u540c\u7684\uff1a\u5b9e\u73b0\u5bf9\u8c61\u7684\u5c01\u88c5\u548c\u4ee3\u7801\u7684\u91cd\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u521b\u5efa\u6784\u9020\u51fd\u6570\u4e3b\u8981\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a\u901a\u8fc7\u51fd\u6570\u58f0\u660e\u5f62\u5f0f\u3001\u901a\u8fc7\u51fd\u6570\u8868\u8fbe\u5f0f\u5f62\u5f0f\u3001\u4ee5\u53ca\u4f7f\u7528 ES6 \u5f15\u5165 [&hellip;]","protected":false},"author":3,"featured_media":170130,"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\/170123"}],"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=170123"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170123\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/170130"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=170123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=170123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=170123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}