{"id":1369,"date":"2022-07-03T22:13:49","date_gmt":"2022-07-03T14:13:49","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1369"},"modified":"2022-07-03T23:03:46","modified_gmt":"2022-07-03T15:03:46","slug":"52-lei-de-ji-cheng-53-shen-me-shi-mixins","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1369","title":{"rendered":"52 &#8211; \u7c7b\u7684\u7ee7\u627f &#038; 53 &#8211; \u4ec0\u4e48\u662f Mixins ?"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16568576537169.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/class-inheritance-l3l\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/class-inheritance-l3l<\/a><\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u5c06\u5bf9\u7c7b\u7684\u7ee7\u627f\u8fdb\u884c\u5927\u6982\u5f97\u4ecb\u7ecd\u3002<\/p>\n<p>\u6211\u4eec\u4f7f\u7528 <code>new<\/code> \u5173\u952e\u5b57\u6765\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u3002\u82e5\u6211\u4eec\u60f3\u7ee7\u627f\u57fa\u7840\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>extends<\/code> \u5173\u952e\u5b57\u3002<\/p>\n<pre><code class=\"language-plain_text\">class Person {\n    name;\n    age;\n\n    constructor(name) {\n        this.name = name;\n        this.age = 33;\n    }\n    getName() {\n        return this.name;\n    }\n}\n\n\/\/ creating new instance of a class Person\nconst person = new Person(&quot;Parwinder&quot;);\nconsole.log(person.getName()); \/\/ Parwinder\n<\/code><\/pre>\n<p>\u6211\u4eec\u4f7f\u7528 <code>extend<\/code> \u6765\u7ee7\u627f Person \u7c7b\uff1a<\/p>\n<pre><code class=\"language-plain_text\">class Person {\n    name;\n    age;\n\n    constructor(name) {\n        this.name = name;\n        this.age = 33;\n    }\n    getName() {\n        return this.name;\n    }\n}\n\nconst person = new Person(&quot;Parwinder&quot;);\nconsole.log(person.getName()); \/\/ Parwinder\n\nclass Female extends Person {\n    gender;\n    constructor(name) {\n        super(name);\n        this.gender = &quot;Female&quot;;\n    }\n}\n\nconst female = new Female(&quot;Lauren&quot;);\n\nconsole.log(female.getName()); \/\/ Lauren\nconsole.log(female instanceof Female); \/\/ true\nconsole.log(female instanceof Person); \/\/ true\n<\/code><\/pre>\n<p><code>super<\/code> \u5173\u952e\u5b57\u8ba9\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u6211\u4eec\u901a\u8fc7\u53ef\u4ee5\u4f7f\u7528 <code>super<\/code> \u6765\u8c03\u7528\u7236\u7c7b\/\u57fa\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<p>ES6 \u4e2d\u7684\u7c7b\u652f\u6301\uff1a<\/p>\n<ul>\n<li>\u539f\u578b\u7ee7\u627f<\/li>\n<li>super \u5173\u952e\u5b57<\/li>\n<li>\u5b9e\u4f8b\u548c\u9759\u6001\u65b9\u6cd5<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16568583136591.jpg\" alt=\"\" \/><\/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>\u4ece\u6211\u535a\u5ba2\u4e2d\u7684\u6240\u6709\u6587\u7ae0\u91cc\uff0c\u6211\u4eec\u5df2\u7ecf\u610f\u8bc6\u5230 JavaScript \u4e2d\u539f\u578b\u7ee7\u627f\u5de5\u4f5c\u7684\u5f88\u597d\uff0c\u8fd9\u662f\u6211\u4eec\u65e9\u5df2\u77e5\u9053\u3002\u53ef\u662f\u6bcf\u4e2a\u5bf9\u8c61\u53ea\u6709\u4e00\u4e2a <code>[[Prototype]]<\/code>\uff0c\u610f\u5473\u7740\u4e00\u4e2a\u5bf9\u8c61\u53ea\u80fd\u4ece\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7ee7\u627f\uff0c\u7c7b\u4e5f\u662f\u4e00\u6837\u53ea\u80fd\u7ee7\u627f\u4e00\u4e2a\u5176\u4ed6\u7c7b\u3002JavaScript \u4e2d\u4e0d\u652f\u6301\u591a\u7ee7\u627f\u3002<\/p>\n<p>mixin \u662f\u4e00\u4e2a\u7c7b\u4e14\u5177\u6709\u80fd\u4f7f\u6211\u4eec\u4ece\u5176\u5b83\u7c7b\u83b7\u53d6\u6240\u9700\u800c\u4e0d\u9700\u8981\u7ee7\u627f mixin \u7c7b\u7684\u65b9\u6cd5\u3002\u8fd9\u5c31\u662f\u5411\u5bf9\u8c61\u6dfb\u52a0\u5c5e\u6027\u800c\u4e0d\u8981\u7ee7\u627f\u3002<\/p>\n<p>\u7406\u8bba\u4e0a\uff0c\u5b83\u5e94\u8be5\u50cf\u8fd9\u6837\uff1a<\/p>\n<ul>\n<li>\u7c7b\u7684\u7ee7\u627f<\/li>\n<\/ul>\n<pre><code class=\"language-plain_text\">class B extends A {}\n<\/code><\/pre>\n<ul>\n<li>\u901a\u8fc7 mixin \u7ee7\u627f<\/li>\n<\/ul>\n<pre><code class=\"language-plain_text\">class B extends A with M1 {}\n<\/code><\/pre>\n<ul>\n<li>\u4ece\u591a\u4e2a mixins \u7ee7\u627f<\/li>\n<\/ul>\n<pre><code class=\"language-plain_text\">class B extends A with M1, M2, M3 {}\n<\/code><\/pre>\n<p>mixins\u7684\u5b8c\u6574\u79d8\u8bc0\u5728\u4e8eObject.assign!<\/p>\n<h2><a id=\"%E5%AE%9E%E7%8E%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5b9e\u73b0<\/h2>\n<h3><a id=\"%E5%AF%B9%E8%B1%A1\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5bf9\u8c61<\/h3>\n<pre><code class=\"language-plain_text\">const employee = {\n    name: &quot;John Smith&quot;,\n    age: 30,\n    gender: &quot;male&quot;\n}\n\nconst payroll = {\n    duration: &quot;monthly&quot;,\n    amount: 7000,\n    currency: &quot;dollars&quot;\n}\n\nconst benefits = {\n    retirement: true,\n    savings: true,\n    health: true,\n    dental: false\n}\n\nconst employeeProfile = Object.assign({}, employee, payroll, benefits);\n\nconsole.log(employeeProfile);\n<\/code><\/pre>\n<p>\u63a7\u5236\u53f0\u5c06\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u4e0b\u9762\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-plain_text\">{ name: 'John Smith',\n  age: 30,\n  gender: 'male',\n  duration: 'monthly',\n  amount: 7000,\n  currency: 'dollars',\n  retirement: true,\n  savings: true,\n  health: true,\n  dental: false \n}\n<\/code><\/pre>\n<p>\u8fd9\u5c31\u662f mixin \u7684\u4f5c\u7528\uff0c\u5b83\u4f7f\u6211\u4eec\u80fd\u805a\u5408\u591a\u4e2a\u4e0d\u540c\u5bf9\u8c61\u7684\u5c5e\u6027\u5230\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u3002<code>Object.assign<\/code> \u4ece\u4e00\u4e2a\u6216\u591a\u4e2a\u5bf9\u8c61\u590d\u5236\u5c5e\u6027\u5230\u76ee\u6807\u5bf9\u8c61\u4e0a\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u6a21\u677f\u5bf9\u8c61\uff0c\u540e\u9762\u7684\u90fd\u662f\u6e90\u5bf9\u8c61\u3002<\/p>\n<h2><a id=\"%E7%B1%BB\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7c7b<\/h2>\n<pre><code class=\"language-plain_text\">let employeeDetails = {\n    returnName() {\n        console.log(`The employee is ${this.name}`);\n    },\n    subscribesToDental () {\n        console.log(`Employee ${this.name} does ${(this.dental) ? &quot;&quot; : &quot;not &quot;}subscribe to dental benefits`);\n    }\n};\n\nclass Employee {\n    name;\n    dental;\n    constructor(name, dental) {\n        this.name = name;\n        this.dental = dental;\n    }\n}\n\nObject.assign(Employee.prototype, employeeDetails);\n\nnew Employee(&quot;Parwinder&quot;, false).returnName();\n\/\/ The employee is Parwinder\nnew Employee(&quot;Parwinder&quot;, false).subscribesToDental();\n\/\/ Employee Parwinder does not subscribe to dental benefits\nnew Employee(&quot;Robert&quot;, true).subscribesToDental();\n\/\/ Employee Robert does subscribe to dental benefits\n<\/code><\/pre>\n<p>\u73b0\u5728\uff0cJavaScript \u652f\u6301 <code>super<\/code> \u5173\u952e\u5b57\u4e86\u3002\u7531\u4e8e <code>super<\/code> \u662f\u8bcd\u6cd5\u7ed1\u5b9a\u6240\u4ee5 mixin \u65e0\u6cd5\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/class-inherita&#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,177,179,178],"class_list":["post-1369","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-class","tag-extend","tag-mixin","tag-178"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1369","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=1369"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1369\/revisions"}],"predecessor-version":[{"id":1370,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1369\/revisions\/1370"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}