{"id":1343,"date":"2022-06-30T09:09:00","date_gmt":"2022-06-30T01:09:00","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1343"},"modified":"2022-06-30T23:06:11","modified_gmt":"2022-06-30T15:06:11","slug":"49-javascript-zhong-de-lei-ji-chu-pian","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1343","title":{"rendered":"49 &#8211; JavaScript \u4e2d\u7684\u7c7b\uff1a\u57fa\u7840\u7bc7"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16565514134606.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/classes-in-javascript-basics-10ip\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/classes-in-javascript-basics-10ip<\/a><\/p>\n<h2><a id=\"%E4%BB%8B%E7%BB%8D\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ecb\u7ecd<\/h2>\n<p>JavaScript \u5f00\u53d1\u8005\u6709\u591a\u79cd\u591a\u6837\u7684\u4ee3\u7801\u98ce\u683c\uff0c\u6709\u7684\u662f\u51fd\u6570\u5f0f\u3001\u6709\u7684\u662f\u9762\u5411\u5bf9\u8c61\u5f0f\u3002\u7c7b\u6216\u8005\u7c7b\u7684\u6784\u9020\u5668\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e00\u90e8\u5206\u3002\u7c7b\u53ef\u4ee5\u770b\u6210\u521b\u5efa\u5bf9\u8c61\u7684\u6a21\u677f\uff0c\u8fd9\u4e2a\u6a21\u677f\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u521d\u59cb\u503c\u548c\u63d0\u4f9b\u4e00\u4e9b\u7279\u5b9a\u7684\u65b9\u6cd5\u3002<\/p>\n<p>JavaScript \u901a\u8fc7\u6784\u9020\u51fd\u6570\u548c new \u64cd\u4f5c\u7b26\u5b8c\u5168\u53ef\u4ee5\u63d0\u4f9b\u8fd9\u79cd\u6a21\u677f\uff0c\u8fd9\u4e5f\u662f\u591a\u6570\u4eba\u628a JavaScript \u4e2d\u7684\u7c7b\u4f5c\u4e3a\u4e00\u4e2a\u8bed\u6cd5\u7cd6\u3002\u8fd9\u5e76\u4e0d\u5b8c\u5168\u6b63\u786e\uff0c\u968f\u7740\u6211\u4eec\u5728\u8fd9\u4e2aJavaScript\u535a\u5ba2\u7cfb\u5217\u4e2d\u7684\u8fdb\u5c55\uff0c\u4f60\u4f1a\u53d1\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<p>\u7c7b\u6216\u7c7b\u5173\u952e\u5b57\u662f\u5728 ES2015\/ES6 \u4e2d\u5f15\u5165\u7684\u3002<\/p>\n<h2><a id=\"%E8%AF%AD%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8bed\u6cd5<\/h2>\n<pre><code class=\"language-plain_text\">class EmployeeRecord {\n    name = &quot;New User&quot;;\n    id = 0;\n\n    constructor(firstName, lastName, id) {\n        this.name = `${firstName} ${lastName}`;\n        this.id = id;\n    }\n}\n\nconst employee1 = new EmployeeRecord(&quot;Parwinder&quot;, &quot;Bhagat&quot;, 1);\nconst employee2 = new EmployeeRecord(&quot;Lauren&quot;, &quot;L&quot;, 2);\n\nconsole.log(employee1.name); \/\/ Parwinder Bhagat\nconsole.log(employee2.name); \/\/ Lauren L\n<\/code><\/pre>\n<p>\u7c7b\u7684\u58f0\u660e\u662f\u901a\u8fc7 <code>class<\/code> \u5173\u952e\u5b57\uff0c\u7136\u540e\u8ddf\u7740\u4e00\u4e2a\u540d\u5b57\u3002\u7c7b\u4f53\u662f\u5305\u542b\u5728\u5927\u62ec\u53f7\u4e2d\u7684\uff0c\u9664\u4e86\u7f3a\u5c11\u53c2\u6570\u5c31\u50cf\u51fd\u6570\u58f0\u660e\u4e00\u6837\u3002<\/p>\n<p>\u53c2\u6570\u662f\u88ab\u4f20\u5230\u6784\u9020\u5668\u4e2d\u7684\uff0c\u5f53\u7c7b\u88ab\u5b9e\u4f8b\u5316\u65f6\u7528\u6765\u521d\u59cb\u5316\u503c\u3002\u628a\u5b83\u60f3\u8c61\u4e3a\u7c7b\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u540c\u65f6\u4e00\u65e6\u7c7b\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5bf9\u8c61\u5b83\u5c31\u4f1a\u7acb\u9a6c\u6267\u884c\u3002\u5728\u7c7b\u4e2d\u53ea\u6709\u4e00\u4e2a\u552f\u4e00\u7684 \u201cconstructor\u201d \u65b9\u6cd5\u3002<\/p>\n<h2><a id=\"%E7%B1%BB%E8%A1%A8%E8%BE%BE%E5%BC%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7c7b\u8868\u8fbe\u5f0f<\/h2>\n<p>\u82e5\u7c7b\u80fd\u88ab\u58f0\u660e\uff0c\u90a3\u5b83\u4eec\u662f\u5426\u4e5f\u5f97\u6709\u8868\u8fbe\u5f0f\u65b9\u5f0f\uff1f\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u53ef\u4ee5\u88ab\u547d\u540d\u4e5f\u53ef\u4ee5\u4e0d\u547d\u540d\u3002\u591a\u6570\u5f00\u53d1\u8005\u4f7f\u7528\u7c7b\u7684\u58f0\u660e\u8bed\u6cd5\u3002<\/p>\n<pre><code class=\"language-plain_text\">let EmployeeRecord = class {\n    name = &quot;New User&quot;;\n    id = 0;\n\n    constructor(firstName, lastName, id) {\n        this.name = `${firstName} ${lastName}`;\n        this.id = id;\n    }\n}\n\nconst employee1 = new EmployeeRecord(&quot;Parwinder&quot;, &quot;Bhagat&quot;, 1);\nconst employee2 = new EmployeeRecord(&quot;Lauren&quot;, &quot;L&quot;, 2);\n\nconsole.log(employee1.name); \/\/ Parwinder Bhagat\nconsole.log(employee2.name); \/\/ Lauren L\n<\/code><\/pre>\n<h2><a id=\"getters%E5%92%8C-setters\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Getters \u548c Setters<\/h2>\n<p>\u6211\u4eec\u901a\u8fc7\u5bf9\u8c61\u7684 getters \u548c setters \u6765\u83b7\u53d6\u6216\u8bbe\u7f6e\u5c5e\u6027\u7684\u503c\uff0c\u6211\u4eec\u4f9d\u65e7\u53ef\u4ee5\u5728\u7c7b\u4e2d\u8fd9\u6837\u4f7f\u7528\u3002<\/p>\n<pre><code class=\"language-plain_text\">let EmployeeRecord = class {\n    firstName = &quot;&quot;;\n    lastName = &quot;&quot;;\n\n    constructor(firstName, lastName) {\n        this.firstName = firstName;\n        this.lastName = lastName;\n    }\n\n    get employeeFullName() {\n        return `${this.firstName} ${this.lastName}`;;\n    }\n\n    set employeeFirstName(firstName) {\n        this.firstName = firstName;\n    }\n\n    set employeeLastName(lastName) {\n        this.lastName = lastName;\n    }\n}\n\nconst employee1 = new EmployeeRecord(&quot;Parwinder&quot;, &quot;Bhagat&quot;);\nconst employee2 = new EmployeeRecord(&quot;Lauren&quot;, &quot;L&quot;);\n\n\/\/ With a getter you do not need the () brackets\nconsole.log(employee1.employeeFullName);\nconsole.log(employee2.employeeFullName);\n\n\/\/ A setter will prohibit someone from outside the class from changing the setter\nemployee1.employeeFullName = &quot;Ricky Ricardo&quot;; \/\/ Cannot assign to 'employeeFullName' because it is a read-only property.\n\n\/\/ But we can use our helpful setter to do so!\n\/\/ Keep in mind that a setter only takes one value\n\nemployee1.employeeFirstName = &quot;Ricky&quot;;\nemployee1.employeeLastName = &quot;Ricardo&quot;;\n\nconsole.log(employee1.employeeFullName); \/\/ Ricky Ricardo\n<\/code><\/pre>\n<p>\u8fd9\u5c31\u662f\u4eca\u5929\u8981\u8bb2\u89e3\u7684\u5185\u5bb9\uff01\u5173\u6ce8\u4e0b\u4e00\u7bc7\u535a\u6587\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u7c7b\u5982\u4f55\u4e0d\u4ec5\u4ec5\u662f\u8bed\u6cd5\u7cd6\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e0e\u51fd\u6570\u6709\u4ec0\u4e48\u4e0d\u540c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/classes-in-jav&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[168,170,169],"class_list":["post-1343","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-class","tag-getter","tag-setter"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1343"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1343\/revisions"}],"predecessor-version":[{"id":1344,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1343\/revisions\/1344"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}