{"id":1383,"date":"2022-07-10T08:15:19","date_gmt":"2022-07-10T00:15:19","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1383"},"modified":"2022-07-10T13:04:24","modified_gmt":"2022-07-10T05:04:24","slug":"58-javascript-zhong-de-duo-tai","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1383","title":{"rendered":"58 &#8211; JavaScript \u4e2d\u7684\u591a\u6001"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16574121534203.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/polymorphism-in-javascript-1960\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/polymorphism-in-javascript-1960<\/a><\/p>\n<p>\u591a\u6001\u662f\u9762\u5411\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2a\u6982\u5ff5\uff0c\u5b83\u662f\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u4e00\u4e2a\u5c5e\u6027\u3001\u65b9\u6cd5\u6216\u5bf9\u8c61\u7684\u80fd\u529b\u3002<\/p>\n<p>\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5177\u6709\u5171\u540c\u529f\u80fd\u7684\u7c7b\u4e2d\u66ff\u4ee3\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u6709\u70b9\u7c7b\u4f3c\u65b9\u6cd5\u91cd\u5199\u3002\u5728\u591a\u6001\u7684\u5e2e\u52a9\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u6807\u51c6\u7684\u63a5\u53e3\u540c\u65f6\u6ee1\u8db3\u7a0d\u5fae\u7684\u5dee\u5f02\u6216\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n<pre><code class=\"language-plain_text\">function Employee(name) {\n    this.name = name;\n    this.getName = () =&gt; {\n        return this.name;\n    }\n}\n\nconst employee = new Employee(&quot;Parwinder&quot;);\n\nconsole.log(employee.getName()); \/\/ Parwinder\n\nEmployee.prototype.getDetails = function () {\n    return this.name.toUpperCase();\n}\n\nconsole.log(employee.getDetails()); \/\/ PARWINDER\n<\/code><\/pre>\n<p>\u4ece\u4e0a\u9762\u4f60\u53ef\u4ee5\u770b\u51fa <code>Employee<\/code> \u5982\u4f55\u6839\u636e\u6211\u4eec\u4f7f\u7528\u5b83\u7684\u5b9e\u4f8b\u7684\u4f4d\u7f6e\u6765\u5b9e\u73b0\u4e00\u4e2a\u6807\u51c6\u7684\u63a5\u53e3\uff0c\u540c\u65f6\u80fd\u8fdb\u884c\u884c\u4e3a\u4e0a\u7a0d\u5fae\u7684\u6539\u52a8\u6765\u6ee1\u8db3\u9700\u6c42\u3002\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u540d\u5b57\u8fd8\u80fd\u83b7\u53d6\u540d\u5b57\u7684\u5927\u5199\u5f62\u5f0f\u3002<\/p>\n<p>\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6765\u8fbe\u5230\u76f8\u540c\u7684\u76ee\u7684\uff1a<\/p>\n<pre><code class=\"language-plain_text\">class Shape {\n    area() {\n        return 0;\n    }\n}\n\nclass Circle extends Shape {\n    constructor (radius) {\n        super(); \/\/ needed to use this\n        this.radius = radius;\n    }\n\n    area() {\n        return Math.PI * this.radius * this.radius;\n    }\n}\n\nclass Rectangle extends Shape {\n    constructor (width, height) {\n        super();\n        this.width = width;\n        this.height = height;\n    }\n\n    area() {\n        return this.width * this.height;\n    }\n}\n\nconst circle = new Circle(2);\nconst rectangle = new Rectangle(5, 6);\n\nconsole.log(circle.area());\nconsole.log(rectangle.area());\n<\/code><\/pre>\n<p>Shape \u662f\u6211\u4eec\u521b\u5efa Circle \u548c Rectangle \u7684\u57fa\u7c7b\u3002\u5b83\u4eec\u90fd\u6709 area \u65b9\u6cd5\u4f46\u662f\u6839\u636e shape \u7c7b\u578b\u7684\u4e0d\u540c\u505a\u4e86\u6539\u52a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/polymorphism-i&#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":[184,178],"class_list":["post-1383","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-184","tag-178"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1383","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=1383"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1383\/revisions"}],"predecessor-version":[{"id":1384,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1383\/revisions\/1384"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}