\u6211\u4eec\u53ef\u4ee5\u5b9e\u4f8b\u5316\u7c7b\u3002<\/ol>\n\u90a3\u4e48\u4e3a\u4ec0\u4e48\u6211\u8bf4\u7c7b\u662f\u8bed\u6cd5\u7cd6\u5462?\u56e0\u4e3a\u5c3d\u7ba1\u5728\u8868\u9762\u4e0a\uff0c\u5b83\u4eec\u770b\u8d77\u6765\u662f\u975e\u5e38\u9762\u5411\u5bf9\u8c61\u7684\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u8bd5\u56fe\u505a\u4e00\u4e9b\u8d85\u51fa\u5b83\u4eec\u53ef\u80fd\u7684\u4e8b\u60c5\uff0c\u6bd4\u5982\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6269\u5c55\u4e24\u4e2a\u7c7b(\u76ee\u524d\u4e0d\u53ef\u80fd\u7684\u4e8b\u60c5)\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801<\/p>\n
\/\/ \u8f85\u52a9\u51fd\u6570 \r\nfunction applyMixins(derivedCtor, baseCtors) { \r\n baseCtors.forEach(baseCtor => { \r\n Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => { \r\n let descriptor = Object.getOwnPropertyDescriptor(baseCtor.prototype, name) \r\n Object.defineProperty(derivedCtor.prototype, name, descriptor); \r\n }); \r\n }); \r\n} \r\n \r\nclass A { \r\n methodA () { \r\n console.log('A') \r\n } \r\n} \r\n \r\nclass B { \r\n methodB () { \r\n console.log('B') \r\n } \r\n} \r\n \r\nclass C { \r\n \r\n} \r\n\/\/ \u4f7f\u7528 mixins\r\n<\/pre>\n\u6211\u4eec\u9700\u8981\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u5728JS\u4e2d\u6211\u4eec\u65e0\u6cd5\u7f16\u5199\uff1a<\/p>\n
class A { \r\n \r\n methodA(){ \r\n console.log(\"A\") \r\n } \r\n} \r\n \r\nclass B { \r\n \r\n methodB(){ \r\n console.log(\"B\") \r\n } \r\n} \r\n \r\nclass C extends A, B { \r\n \r\n}\r\n<\/pre>\n\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5173\u952e\u90e8\u5206\u5e94\u8be5\u662fapplyMixins\u51fd\u6570\u3002\u5982\u679c\uff0c\u4f60\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\u5b83\u8bd5\u56fe\u505a\u4ec0\u4e48\uff0c\u4f46\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230\u5b83\u6b63\u5728\u8bbf\u95ee\u6240\u6709\u7c7b\u7684\u539f\u578b\u5c5e\u6027\u6765\u590d\u5236\u548c\u91cd\u65b0\u5206\u914d\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u8fd9\u5c31\u662f\u6211\u4eec\u9700\u8981\u770b\u5230\u771f\u76f8\u7684\u5730\u65b9:\u7c7b\u53ea\u4e0d\u8fc7\u662f\u5728\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u539f\u578b\u7ee7\u627f\u6a21\u578b\u4e4b\u4e0a\u7684\u8bed\u6cd5\u7cd6\u3002<\/p>\n
\u8fd9\u662f\u5426\u610f\u5473\u7740\u6211\u4eec\u5e94\u8be5\u505c\u6b62\u4f7f\u7528\u7c7b?\u5f53\u7136\u4e0d\u662f\uff0c\u91cd\u8981\u7684\u662f\u8981\u7406\u89e3\u5b83\uff0c\u800c\u4e14\u5982\u679c\u6211\u4eec\u60f3\u505a\u4e9b\u7a81\u7834\u7c7b\u7684\u9650\u5236\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5fc5\u987b\u7528\u539f\u578b\u6765\u5904\u7406\u3002<\/p>\n
JS \u7684OOP \u6a21\u578b\u7f3a\u5931\u4e86\u4ec0\u4e48\u5462?<\/strong><\/div>\n\u5982\u679c\u6211\u4eec\u5f53\u524d\u7684OOP\u6a21\u578b\u662f\u5982\u6b64\u4e4b\u8584\uff0c\u4ec5\u662f\u539f\u578b\u7ee7\u627f\u7684\u62bd\u8c61\u5c42\uff0c\u90a3\u4e48\u6211\u4eec\u5230\u5e95\u7f3a\u5c11\u4ec0\u4e48\u5462?\u662f\u4ec0\u4e48\u8ba9JS\u771f\u6b63\u6210\u4e3aOOP?<\/p>\n
\u770b\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e2a\u597d\u65b9\u6cd5\u5c31\u662f\u770b\u770bTypeScript\u5728\u505a\u4ec0\u4e48\u3002\u8be5\u8bed\u8a00\u80cc\u540e\u7684\u56e2\u961f\u901a\u8fc7\u521b\u5efa\u4e00\u4e9b\u53ef\u4ee5\u7ffb\u8bd1\u6210JS\u7684\u4e1c\u897f\uff0c\u65e0\u7591\u5c06 JS \u63a8\u5411\u4e86\u6781\u9650\u3002\u8fd9\u53cd\u8fc7\u6765\u4e5f\u9650\u5236\u4e86\u5b83\u4eec\u7684\u80fd\u529b\u3002<\/p>\n
\u76ee\u524d JS \u4e2d\u7f3a\u5931\u7684\u4e00\u4e9bOOP\u6784\u9020\u5177\u6709\u5185\u5728\u7684\u7c7b\u578b\u68c0\u67e5\u529f\u80fd\uff0c\u5728\u52a8\u6001\u7c7b\u578b\u8bed\u8a00\u4e2d\u6ca1\u6709\u771f\u6b63\u7684\u610f\u4e49\uff0c\u8fd9\u53ef\u80fd\u662f\u5b83\u4eec\u8fd8\u6ca1\u6709\u88ab\u6dfb\u52a0\u7684\u539f\u56e0\u3002<\/p>\n
\u63a5\u53e3<\/strong><\/div>\n\u63a5\u53e3\u53ef\u5e2e\u52a9\u5b9a\u4e49\u7c7b\u5e94\u9075\u5faa\u7684API\u3002\u63a5\u53e3\u7684\u4e3b\u8981\u597d\u5904\u4e4b\u4e00\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5b9e\u73b0\u76f8\u540c\u63a5\u53e3\u7684\u4efb\u4f55\u7c7b\u7684\u53d8\u91cf\uff0c\u7136\u540e\u5b89\u5168\u5730\u8c03\u7528\u5176\u4efb\u4f55\u65b9\u6cd5\u3002<\/p>\n
interface Animal { \r\n speak() \r\n} \r\n \r\nclass Dog implements Animal{ \r\n speak() { \r\n console.log(\"Woof!\") \r\n } \r\n} \r\n \r\nclass Cat implements Animal{ \r\n speak() { \r\n console.log(\"Meau!\") \r\n } \r\n} \r\n \r\nclass Human implements Animal{ \r\n speak() { \r\n console.log(\"Hey dude, what's up?\") \r\n } \r\n} \r\n<\/pre>\n\/\/\u5982\u679c\u6211\u4eec\u5728JS\u4e2d\u6709\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u505a\uff1a
\nlet objects = [new Dog(), new Cat(), new Human()]
\nobjects.forEach(o => o.speak())
\n\u5f53\u7136\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49speak\u65b9\u6cd5\u5e76\u8986\u76d6\u5b83\u7684\u7c7b\u6765\u5b9e\u73b0\u540c\u6837\u7684\u76ee\u7684\uff0c\u4f46\u63a5\u53e3\u66f4\u52a0\u6e05\u6670\u548c\u4f18\u96c5\u3002<\/p>\n
\u62bd\u8c61\u7c7b<\/strong><\/div>\n\u6bcf\u5f53\u6211\u5c1d\u8bd5\u5bf9\u6211\u7684\u4ee3\u7801\u8fdb\u884c\u5b8c\u6574\u7684OOP\u64cd\u4f5c\u65f6\uff0c\u6211\u80af\u5b9a\u4f1a\u9519\u8fc7JS\u4e2d\u7684\u62bd\u8c61\u7c7b\u3002\u62bd\u8c61\u7c7b\u662f\u5b9a\u4e49\u548c\u5b9e\u73b0\u65b9\u6cd5\u7684\u7c7b\uff0c\u4f46\u6c38\u8fdc\u4e0d\u4f1a\u5b9e\u4f8b\u5316\u3002\u8fd9\u662f\u4e00\u79cd\u53ef\u4ee5\u6269\u5c55\u4f46\u4ece\u672a\u76f4\u63a5\u4f7f\u7528\u7684\u5e38\u89c1\u884c\u4e3a\u7684\u5206\u7ec4\u65b9\u5f0f\u3002\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u8d44\u6e90\uff0c\u5e76\u4e14\u7edd\u5bf9\u53ef\u4ee5\u5728\u5f53\u524dJS\u9886\u57df\u5185\u5b9e\u73b0\u800c\u4e0d\u4f1a\u82b1\u8d39\u592a\u591a\u7cbe\u529b\u3002<\/p>\n
\u9759\u6001\u591a\u6001<\/strong><\/div>\n\u9759\u6001\u591a\u6001\u6027\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u76f8\u540c\u7684\u7c7b\u4e2d\u591a\u6b21\u5b9a\u4e49\u76f8\u540c\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u5177\u6709\u4e0d\u540c\u7684\u7b7e\u540d\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u91cd\u590d\u8be5\u540d\u79f0\uff0c\u4f46\u8981\u786e\u4fdd\u5176\u63a5\u6536\u4e0d\u540c\u7684\u53c2\u6570\u3002\u73b0\u5728\u6211\u4eec\u6709\u4e86JS\u7684rest\u53c2\u6570\uff0c\u8fd9\u4f7f\u6211\u4eec\u53ef\u4ee5\u62e5\u6709\u4e00\u4e2a\u4efb\u610f\u6570\u5b57\uff0c\u4f46\u662f\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u6211\u4eec\u5fc5\u987b\u5728\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u4ee3\u7801\u6765\u5904\u7406\u8fd9\u79cd\u52a8\u6001\u6027\u3002\u76f8\u53cd\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u533a\u5206\u65b9\u6cd5\u7b7e\u540d\uff0c\u5219\u53ef\u4ee5\u5c06\u76f8\u540c\u884c\u4e3a\u7684\u4e0d\u540c\u542b\u4e49\u76f4\u63a5\u5c01\u88c5\u5230\u4e0d\u540c\u65b9\u6cd5\u4e2d\u3002<\/p>\n
<\/p>\n
\u5de6\u8fb9\u7684\u7248\u672c\u4e0d\u662f\u6709\u6548\u7684JS\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u5e72\u51c0\u7684\u4ee3\u7801\uff0c\u56e0\u6b64\uff0c\u9605\u8bfb\u548c\u7406\u89e3\u8d77\u6765\u6bd4\u8f83\u5bb9\u6613\u3002\u53f3\u8fb9\u7684\u7248\u672c\u662f\u5b8c\u5168\u6709\u6548\u7684\uff0c\u5b83\u9605\u8bfb\u8d77\u6765\u76f8\u5bf9\u56f0\u96be\u4e9b\uff0c\u8fd8\u8981\u61c2\u5f97\u4e00\u4e9b ES6 \u7684\u8bed\u6cd5\u3002<\/p>\n
\u591a\u6001\u6027\u901a\u5e38\u662f\u901a\u8fc7\u67e5\u770b\u65b9\u6cd5\u4e2d\u63a5\u6536\u5230\u7684\u53c2\u6570\u7684\u7c7b\u578b\u6765\u5b9e\u73b0\u7684\u3002\u4f46\u662f\uff0c\u7531\u4e8eJS\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u77e5\u9053\u8fd9\u662f\u4e0d\u53ef\u80fd\u7684\u3002<\/p>\n
\u53d7\u4fdd\u62a4\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/div>\n\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u516c\u5f00\u7684\u53ef\u89c1\u6027\uff0c\u800c\u4e14\u6211\u4eec\u5f88\u5feb\u5c31\u5f97\u5230\u4e86\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u79c1\u6709\u53ef\u89c1\u6027(\u901a\u8fc7#\u524d\u7f00)\u3002\u6211\u8ba4\u4e3a\u4e0b\u4e00\u6b65\u5e94\u8be5\u662f\u6dfb\u52a0\u53d7\u4fdd\u62a4\u7684\u53ef\u89c1\u6027\uff0c\u7136\u800c\uff0c\u73b0\u5728\u8fd8\u6ca1\u6709\uff0c\u6211\u8ba4\u4e3a\u5982\u679c\u4f60\u60f3\u8981\u6709\u4e00\u4e2a\u5408\u9002\u7684OOP\u4f53\u9a8c\uff0c\u8fd9\u4e09\u4e2a\u90fd\u662f\u5fc5\u8981\u7684\u3002\u53d7\u4fdd\u62a4\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u53ea\u80fd\u4ece\u7c7b\u5185\u90e8\u6216\u5b83\u7684\u4e00\u4e2a\u5b50\u7c7b\u4e2d\u8bbf\u95ee(\u4e0e\u79c1\u6709\u53ef\u89c1\u6027\u76f8\u53cd\uff0c\u79c1\u6709\u53ef\u89c1\u6027\u5c06\u8bbf\u95ee\u9650\u5236\u4e3a\u53ea\u80fd\u8bbf\u95ee\u7236\u7c7b)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u4ee5\u6211\u7684\u62d9\u89c1\uff0c\u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u662f\uff1a\u6ca1\u6709\u3002\u4f46\u662f\u793e\u533a\u82b1\u4e86\u5f88\u591a\u5e74\u7684\u65f6\u95f4\u624d\u5c06\u7c7b\u7684\u6982\u5ff5\u5f3a\u52a0\u5230\u4e0d\u540c\u7684\u7ed3\u6784\u548c\u5e93\u4e2d\uff0c\u56e0\u6b64ECMA\u6280 […]<\/p>\n","protected":false},"author":1329,"featured_media":211403,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-211401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=211401"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211401\/revisions"}],"predecessor-version":[{"id":211405,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/211401\/revisions\/211405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/211403"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=211401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=211401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=211401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}