{"id":1346,"date":"2022-07-01T06:29:45","date_gmt":"2022-06-30T22:29:45","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1346"},"modified":"2022-07-02T00:06:27","modified_gmt":"2022-07-01T16:06:27","slug":"50-lei-bu-jin-jin-shi-yu-fa-tang","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1346","title":{"rendered":"50 &#8211; \u7c7b\uff0c\u4e0d\u4ec5\u4ec5\u662f\u8bed\u6cd5\u7cd6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16566282230242.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/classes-not-just-syntactic-sugar-1n0m\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/classes-not-just-syntactic-sugar-1n0m<\/a><\/p>\n<p>\u4e0a\u7bc7\u6587\u7ae0\u6211\u4eec\u63d0\u5230\u4e86\u7c7b\u4ee5\u53ca\u5b83\u662f\u5982\u4f55\u6839\u636e\u6a21\u677f\u66f4\u7b80\u5355\u7684\u521b\u5efa\u5bf9\u8c61\u3002<code>class<\/code> \u5173\u952e\u5b57\u662f\u5728 ES2015\/ES6 \u4e2d\u5f15\u5165\u7684\uff0c\u6709\u4e2a\u5e38\u89c1\u7684\u8bef\u533a\u7c7b\u53ea\u662f\u8bed\u6cd5\u7cd6\u4ec5\u6b64\u800c\u5df2\u3002\u7c7b\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\uff0c\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\u7684\u76ee\u7684\u662f\u9610\u660e\u8fd9\u4e2a\u8bef\u533a\u548c\u5c55\u793a\u4e0e\u901a\u8fc7 <code>new<\/code> \u5173\u952e\u5b57\u521b\u5efa\u5bf9\u8c61\u7684\u7ec6\u5fae\u5dee\u522b\u3002<\/p>\n<h2><a id=\"%E7%B1%BB%E9%83%BD%E5%81%9A%E4%BA%86%E4%BB%80%E4%B9%88%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7c7b\u90fd\u505a\u4e86\u4ec0\u4e48\uff1f<\/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    reverseName() {\n        return this.name.split(&quot;&quot;).reverse().join(&quot;&quot;);\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\nconsole.log(employee1.reverseName()); \/\/ tagahB redniwraP\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684 <code>class<\/code> \u4f8b\u5b50\u4e2d\uff1a<\/p>\n<ol>\n<li>\u5728\u5e95\u5c42\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a <code>EmployeeRecord<\/code> \u7684\u51fd\u6570\uff0c\u51fd\u6570\u4f53\u5c31\u662f\u7531\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7ec4\u6210\u3002\u5982\u679c\u6ca1\u6709\u6784\u9020\u51fd\u6570\uff0c\u51fd\u6570\u4f53\u5c06\u662f\u7a7a\u7684\u3002<\/li>\n<li>\u7c7b\u4e2d\u6240\u6709\u7684\u51fd\u6570\u90fd\u4f1a\u88ab\u5b58\u50a8\u5728 <code>EmployeeRecord<\/code> \u7684\u539f\u578b\u4e0a\u3002<\/li>\n<\/ol>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u903b\u8f91\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u7528 <code>class<\/code> \u5173\u952e\u5b57\u6765\u91cd\u5199\u4e0a\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n<pre><code class=\"language-plain_text\">function EmployeeRecord(firstName, lastName, id) {\n    this.name = `${firstName} ${lastName}`;\n    this.id = id;\n}\n\nEmployeeRecord.prototype.reverseName = function () {\n    return this.name.split(&quot;&quot;).reverse().join(&quot;&quot;);\n}\n\nlet 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\nconsole.log(employee1.reverseName()); \/\/ tagahB redniwraP\n<\/code><\/pre>\n<h2><a id=\"%E7%B1%BB%E6%9C%89%E4%BD%95%E4%B8%8D%E5%90%8C%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7c7b\u6709\u4f55\u4e0d\u540c\uff1f<\/h2>\n<ul>\n<li>\u6709\u4e00\u4e2a\u7279\u5b9a\u7684\u51fd\u6570\u79cd\u7c7b\u5206\u914d\u7ed9\u7c7b\uff0c\u5e76\u4e14\u5728\u591a\u4e2a\u5730\u65b9\u88ab\u68c0\u67e5\uff0c\u6700\u91cd\u8981\u7684\u662f\u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\u65f6\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-plain_text\">class EmployeeRecord {\n    constructor() { }\n}\n\nconsole.log(typeof EmployeeRecord); \/\/ function\nEmployeeRecord(); \/\/ Value of type 'typeof EmployeeRecord' is not callable. Did you mean to include 'new'?\n<\/code><\/pre>\n<ul>\n<li>\u6784\u9020\u51fd\u6570\u5f0f\u7684\u7ee7\u627f\u4f9d\u8d56\u539f\u578b\u94fe\uff0c\u800c\u7c7b\u5219\u662f\u901a\u8fc7 <code>extends<\/code> \u5173\u952e\u5b57\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-plain_text\">class Person {\n    sayName() {\n        console.log(&quot;My name is Person&quot;);\n    }\n\n    sayAge() {\n        console.log(&quot;I am 30 years old.&quot;); \/\/ I am 30 years old.\n    }\n}\n\nclass Employee extends Person {\n    sayDepartment() {\n        console.log(&quot;I work for the tech department.&quot;); \/\/ I work for the tech department.\n    }\n\n    sayHello() {\n        console.log(&quot;Hi, I am the new employee&quot;); \/\/ Hi, I am the new employee\n    }\n}\n\nlet employee = new Employee;\n\nemployee.sayHello();\nemployee.sayAge();\nemployee.sayDepartment();\n\nconsole.log(employee instanceof Person); \/\/ true\nconsole.log(employee instanceof Employee); \/\/ true\n<\/code><\/pre>\n<ul>\n<li>\u51fd\u6570\u58f0\u660e\u662f\u4f1a\u88ab\u63d0\u5347\uff0c\u800c\u7c7b\u7684\u58f0\u660e\u4e0d\u4f1a\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-plain_text\">const employee = new Employee(); \/\/ ReferenceError or Employee is not a constructor\n\nclass Employee {\n    constructor() {}\n}\n<\/code><\/pre>\n<ul>\n<li>\u7c7b\u59cb\u7ec8\u8fd0\u884c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7c7b\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u81ea\u52a8\u90fd\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u3002<\/li>\n<li>\u51fd\u6570\u58f0\u660e\u6216\u8868\u8fbe\u5f0f\u53ef\u4ee5\u88ab\u8986\u76d6\u5c31\u50cf <code>var<\/code> \u58f0\u660e\u7684\u53d8\u91cf\u800c\u7c7b\u5219\u4e0d\u884c\u3002\u7c7b\u5c31\u50cf <code>let<\/code> \u548c <code>const<\/code> \u5173\u952e\u5b57\u58f0\u660e\u7684\u53d8\u91cf\uff0clet \u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u662f\u4e0d\u5141\u8bb8\u58f0\u660e\u540c\u540d\u53d8\u91cf\u7684\u3002<\/li>\n<li>\u5bf9\u8c61\u7684\u4e0d\u53ef\u679a\u4e3e\u5c5e\u6027\u5728\u8fed\u4ee3\u7684\u65f6\u5019\u4e0d\u4f1a\u51fa\u73b0\uff0c\u7c7b\u4e2d\u7684\u65b9\u6cd5\u4e0d\u4f1a\u88ab\u8fed\u4ee3\u5230\u3002\u82e5\u6211\u4eec\u4f7f\u7528 <code>for...in<\/code> \u6765\u904d\u5386\u7c7b\u4e2d\u7684\u5bf9\u8c61\uff0c\u4e0d\u4f1a\u6709\u65b9\u6cd5\u51fa\u73b0\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/classes-not-ju&#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,6],"class_list":["post-1346","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-class","tag-javascript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1346","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=1346"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1346\/revisions"}],"predecessor-version":[{"id":1347,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1346\/revisions\/1347"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}