{"id":387,"date":"2019-01-06T17:36:09","date_gmt":"2019-01-06T09:36:09","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=387"},"modified":"2019-01-06T17:36:09","modified_gmt":"2019-01-06T09:36:09","slug":"ch-9-%e6%b7%b7%e5%90%88%e7%9a%84%e3%80%8c%e9%a1%9e%e5%88%a5%e3%80%8d%e7%89%a9%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=387","title":{"rendered":"Ch 9. \u6df7\u5408\u7684\u300c\u985e\u5225\u300d\u7269\u4ef6"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_75 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"\u986f\u793a\/\u96b1\u85cf\u5167\u5bb9\u76ee\u9304\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E9%A1%9E%E5%88%A5%E7%90%86%E8%AB%96\" >\u985e\u5225\u7406\u8ad6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D%E8%A8%AD%E8%A8%88%E6%A8%A1%E5%BC%8F\" >\u300c\u985e\u5225\u300d\u8a2d\u8a08\u6a21\u5f0f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#JavaScript%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D\" >JavaScript\u300c\u985e\u5225\u300d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E5%BB%BA%E7%BD%AE\" >\u5efa\u7f6e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E5%BB%BA%E6%A7%8B%E5%99%A8\" >\u5efa\u69cb\u5668<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E9%A1%9E%E5%88%A5%E7%9A%84%E7%B9%BC%E6%89%BF\" >\u985e\u5225\u7684\u7e7c\u627f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E5%A4%9A%E5%9E%8B\" >\u591a\u578b<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E5%A4%9A%E9%87%8D%E7%B9%BC%E6%89%BF\" >\u591a\u91cd\u7e7c\u627f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#Mixins\" >Mixins<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E6%98%8E%E7%A2%BA%E7%9A%84_Mixins\" >\u660e\u78ba\u7684 Mixins<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E5%AF%84%E7%94%9F%E5%BC%8F%E7%B9%BC%E6%89%BF_Parasitic_Inheritance\" >\u5bc4\u751f\u5f0f\u7e7c\u627f (Parasitic Inheritance)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E9%9A%B1%E5%90%AB%E7%9A%84_Mixins\" >\u96b1\u542b\u7684 Mixins<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=387\/#%E8%A4%87%E7%BF%92\" >\u8907\u7fd2<\/a><\/li><\/ul><\/nav><\/div>\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%A1%9E%E5%88%A5%E7%90%86%E8%AB%96\"><\/span><strong>\u985e\u5225\u7406\u8ad6<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>OO \u6216\u985e\u5225\u5c0e\u5411\u6240\u5f37\u8abf\u7684\u662f\uff0c\u8cc7\u6599\u672c\u8eab\u5c31\u5177\u6709\u95dc\u806f\u884c\u70ba\u80fd\u5920\u4f5c\u7528\u5728\u5b83\u5011\u8eab\u4e0a\uff0c\u6240\u4ee5\u8981\u7d93\u904e\u9069\u7576\u7684\u8a2d\u8a08\uff0c\u5c07\u8cc7\u6599\u53ca\u884c\u70ba\u6253\u5305\u5728\u4e00\u8d77\u3002<\/p>\n\n\n<p>\u8209\u4f8b\u4f86\u8aaa\uff0c\u4e00\u4e32\u5b57\u5143\u88ab\u7a31\u70ba\u5b57\u4e32 (String)\uff0c\u5b57\u5143\u5c31\u662f\u8cc7\u6599\uff0c\u800c\u5957\u7528\u5230\u8cc7\u6599\u7684\u884c\u70ba (\u8a08\u7b97\u9577\u5ea6\u3001\u641c\u5c0b\u3001\u5207\u5206\u7b49\u7b49) \u90fd\u88ab\u8a2d\u8a08\u70ba String \u985e\u5225\u7684\u65b9\u6cd5\u3002\u4efb\u4f55\u7d66\u5b9a\u7684\u5b57\u4e32\u90fd\u53ea\u662f\u985e\u5225\u7684\u4e00\u500b\u5be6\u9ad4 (instance)\u3002<\/p>\n\n\n<p>\u6700\u5e38\u898b\u7684\u4f8b\u5b50\uff0c\u4e00\u90e8\u8eca\u5b50\u6216\u662f\u706b\u8eca\u53ef\u4ee5\u88ab\u8996\u70ba\u67d0\u500b\u985e\u5225\u7684\u5177\u9ad4\u5be6\u4f5c\uff0c\u4f8b\u5982 Vehicle (\u8f09\u5177)\u3002\u8eca\u5b50\u8207\u706b\u8eca\u6703\u6709\u5171\u901a\u7684\u5c6c\u6027\u6216\u884c\u70ba\uff0c\u4f8b\u5982\u8f2a\u5b50\u3001\u63a8\u9032\u3002\u800c\u5728\u8edf\u9ad4\u4e2d\uff0c\u70ba\u4e0d\u540c\u985e\u578b\u7684\u8f09\u5177\u91cd\u8907\u5b9a\u7fa9\u57fa\u672c\u8981\u7d20\uff0c\u4e26\u4e0d\u5408\u7406\u3002\u56e0\u6b64\u6211\u5011\u6703\u5728 Vehicle \u4e0a\u5b9a\u7fa9\u4e00\u6b21\uff0c\u7576\u5b9a\u7fa9 Car \u7684\u6642\u5019\uff0c\u518d\u7e7c\u627f\u6216\u64f4\u5145 Vehicle\u3002<\/p>\n\n\n<p>\u53e6\u4e00\u500b\u95dc\u9375\u6982\u5ff5\u662f\u591a\u578b (polymorphism)\uff0c\u4f86\u81ea\u7236\u985e\u5225\u7684\u884c\u70ba\u53ef\u4ee5\u5728\u5b50\u985e\u5225\u4e2d\u88ab\u8986\u5beb (overridden)\uff0c\u4ee5\u8ce6\u4e88\u5b83\u7279\u5316\u7684\u884c\u70ba\u3002\u7279\u5b9a\u884c\u70ba\u61c9\u8a72\u5171\u7528\u65b9\u6cd5\u540d\u7a31\uff0c\u5982\u6b64\u624d\u80fd\u8986\u5beb\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D%E8%A8%AD%E8%A8%88%E6%A8%A1%E5%BC%8F\"><\/span>\u300c\u985e\u5225\u300d\u8a2d\u8a08\u6a21\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u5e38\u898b\u7684\u71b1\u9580\u8a0e\u8ad6\u662f OO \u7684\u8a2d\u8a08\u6a21\u5f0f\uff0c\u9019\u6a23\u7684\u60c5\u6cc1\u4e0b\uff0c\u5e7e\u4e4e\u662f\u5047\u8a2d\u4e86 OO \u662f\u7528\u4f86\u5be6\u73fe\u8a2d\u8a08\u6a21\u5f0f\u7684\u6a5f\u5236\uff0c\u662f\u7a0b\u5f0f\u7684\u57fa\u790e\u3002\u7136\u800c\u985e\u5225<strong>\u53ea\u4e0d\u904e\u662f\u6578\u7a2e\u5e38\u898b\u7684\u8a2d\u8a08\u6a21\u5f0f\u4e4b\u4e00<\/strong>\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JavaScript%E3%80%8C%E9%A1%9E%E5%88%A5%E3%80%8D\"><\/span>JavaScript\u300c\u985e\u5225\u300d<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>JS \u5177\u6709 <code>new<\/code>\u3001<code>instanceof<\/code>\u3001<code>class<\/code> \u7b49\u95dc\u9375\u5b57\u5df2\u7d93\u597d\u4e00\u9663\u5b50\u4e86\uff0c\u4f46\u5b83\u5be6\u969b\u4e0a\u4e26\u4e0d\u5177\u6709\u985e\u5225\u3002\u53ea\u662f\u8a66\u8457\u6eff\u8db3\u300c\u60f3\u8981\u4ee5\u985e\u5225\u9032\u884c\u8a2d\u8a08\u300d\u9019\u7a2e\u6e34\u671b\u800c\u63d0\u4f9b\u7684\u8a9e\u6cd5\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BB%BA%E7%BD%AE\"><\/span>\u5efa\u7f6e<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u300c\u985e\u5225 (class)\u300d\u8207\u300c\u5be6\u9ad4 (instance)\u300d\u7684\u601d\u7dad\uff0c\u6e90\u81ea\u65bc\u5efa\u7bc9\u5de5\u7a0b\u3002\u5efa\u7bc9\u5e2b\u6703\u898f\u756b\u5efa\u7bc9\u7269\u7684\u6240\u6709\u7279\u5fb5\uff0c\u4f46\u4ed6\u4e26\u4e0d\u5728\u610f\u5efa\u7bc9\u7269\u6703\u5728\u4f55\u8655\u5efa\u69cb\u591a\u5c11\u68df\u3002\u4ed6\u4e5f\u4e0d\u5728\u610f\u5efa\u7bc9\u7269\u7684\u5167\u5bb9\uff0c\u53ea\u5728\u610f\u5176\u4e2d\u7684\u7d50\u69cb\u3002\u4e4b\u5f8c\uff0c\u6211\u5011\u6703\u900f\u904e\u5efa\u9020\u5546\u5c07\u85cd\u5716\u5be6\u73fe\uff0c\u88fd\u9020\u51fa\u8907\u88fd\u54c1\uff0c\u800c\u4e14\u53ef\u4ee5\u79fb\u52d5\u5230\u9694\u58c1\u7684\u7a7a\u5730\uff0c\u518d\u5efa\u9020\u51fa\u53e6\u4e00\u500b\u8907\u88fd\u54c1\u3002<\/p>\n\n\n<p>\u985e\u5225\u5c31\u662f\u85cd\u5716\uff0c\u800c\u5be6\u969b\u80fd\u5920\u8207\u4e4b\u4e92\u52d5\u7684\u7269\u4ef6\uff0c\u5247\u662f\u5be6\u9ad4\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%BB%BA%E6%A7%8B%E5%99%A8\"><\/span>\u5efa\u69cb\u5668<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u5efa\u69cb\u5668\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u65b9\u6cd5 (function)\uff0c\u8ca0\u8cac\u521d\u59cb\u5316\u5be6\u9ad4\u6703\u9700\u8981\u7684\u4efb\u4f55\u8cc7\u8a0a\u3002\u4e00\u500b\u985e\u5225\u7684\u5efa\u69cb\u5668\u5c6c\u65bc\u8a72\u985e\u5225\uff0c\u800c\u4e14\u7e3d\u662f\u8207\u8a72\u985e\u5225\u540c\u540d\u3002\u4f7f\u7528 <code>new<\/code> \u4f86\u5efa\u69cb\u6642\u6703\u540c\u6642\u547c\u53eb\u5efa\u69cb\u5668\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%A1%9E%E5%88%A5%E7%9A%84%E7%B9%BC%E6%89%BF\"><\/span><strong>\u985e\u5225\u7684\u7e7c\u627f<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u5728\u985e\u5225\u5c0e\u5411\u7684\u8a9e\u8a00\u4e2d\uff0c\u4e0d\u53ea\u80fd\u5920\u5b9a\u7fa9\u985e\u5225\uff0c\u9084\u53ef\u4ee5\u5b9a\u7fa9\u7e7c\u627f\u81ea\u5176\u4ed6\u985e\u5225\u7684\u985e\u5225\u3002\u7e7c\u627f\u4f86\u7684\u985e\u5225\u901a\u5e38\u88ab\u7a31\u70ba\u300c\u5b50\u985e\u5225 (child class)\u300d\uff0c\u88ab\u7e7c\u627f\u7684\u5247\u662f\u300c\u7236\u985e\u5225 (parent class)\u300d\u3002\u7576\u5b50\u985e\u5225\u5b9a\u7fa9\u5b8c\u7562\uff0c\u5b83\u5c31\u6703\u8207\u7236\u985e\u5225\u5206\u96e2\uff0c\u6210\u70ba\u4e0d\u540c\u7684\u985e\u5225\u3002\u5b50\u985e\u5225\u6703\u542b\u6709\u5f9e\u7236\u985e\u5225\u8907\u88fd\u904e\u4f86\u7684\u6700\u521d\u7684\u884c\u70ba\uff0c\u4f46\u5728\u4e4b\u5f8c\u53ef\u4ee5\u8986\u5beb (override)\u3002<\/p>\n\n\n<p>P.150 \u7bc4\u4f8b\u3002Car \u548c SpeedBoat \u90fd\u7e7c\u627f\u4e86 Vehicle \u7684\u901a\u7528\u7279\u5fb5\uff0c\u4f46\u7279\u5316\u51fa\u4e86\u9069\u5408\u81ea\u5df1\u7a2e\u985e\u7684\u7279\u5fb5\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%A4%9A%E5%9E%8B\"><\/span>\u591a\u578b<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u591a\u578b\u662f\u6307\u4efb\u4f55\u65b9\u6cd5\u90fd\u80fd\u53c3\u8003\u5230\u8f03\u9ad8\u968e\u5c64\u4e2d\u7684\u53e6\u4e00\u500b\u65b9\u6cd5\u3002\u5982 Car \u5b9a\u7fa9\u4e86\u81ea\u5df1\u7684 drive() \u65b9\u6cd5\uff0c\u4f46\u5b83\u9084\u662f\u80fd\u53c3\u8003\u5230\u5b83\u6240\u7e7c\u627f\u7684 (Vehicle) \u7684 drive()\u3002\u81f3\u65bc\u6703\u63a1\u7528\u54ea\u500b\u7248\u672c\u7684\u65b9\u6cd5\uff0c\u6703\u6839\u64da\u4f60\u6240\u53c3\u8003\u7684\u5be6\u9ad4\u662f\u54ea\u500b\u985e\u5225\u800c\u4f86\u6c7a\u5b9a\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%A4%9A%E9%87%8D%E7%B9%BC%E6%89%BF\"><\/span>\u591a\u91cd\u7e7c\u627f<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u7d93\u5178\u7684\u947d\u77f3\u554f\u984c\uff0c\u7576\u4e00\u500b\u5b50\u985e\u5225\u7e7c\u627f\u4e86\u5169\u500b\u7236\u985e\u5225\uff0c\u7576\u5176\u53c3\u8003\u4e00\u500b\u65b9\u6cd5\u6642\uff0c\u8981\u63a1\u7528\u54ea\u500b\u7248\u672c\u5462\uff1f\u5728 JS \u4e4b\u4e2d\uff0c\u4e26\u6c92\u6709\u63d0\u4f9b\u539f\u751f\u6a5f\u5236\u4f86\u9032\u884c\u591a\u91cd\u7e7c\u627f\u3002<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/github.com\/getify\/You-Dont-Know-JS\/raw\/master\/this%20%26%20object%20prototypes\/fig2.png\" alt=\"\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Mixins\"><\/span><strong>Mixins<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u7269\u4ef6\u4e26\u4e0d\u6703\u5728\u7e7c\u627f\u6216\u5be6\u9ad4\u5316\u6642\u81ea\u52d5\u8907\u88fd\u884c\u70ba\uff0c\u7269\u4ef6\u53ea\u6703\u88ab\u9023\u7d50\u5728\u4e00\u8d77\u3002\u958b\u767c\u4eba\u54e1\u53ef\u4ee5\u900f\u904e Mixins \u4f86\u507d\u9020\u8907\u88fd\u7684\u52d5\u4f5c\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%98%8E%E7%A2%BA%E7%9A%84_Mixins\"><\/span>\u660e\u78ba\u7684 Mixins<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ vastly simplified `mixin(..)` example:\nfunction mixin( sourceObj, targetObj ) {\n    for (var key in sourceObj) {\n        \/\/ only copy if not already present\n        if (!(key in targetObj)) {\n            targetObj[key] = sourceObj[key];\n        }\n    }\n    return targetObj;\n}\nvar Vehicle = {\n    engines: 1,\n    ignition: function() {\n        console.log( \"Turning on my engine.\" );\n    },\n    drive: function() {\n        this.ignition();\n        console.log( \"Steering and moving forward!\" );\n    }\n};\nvar Car = mixin( Vehicle, {\n    wheels: 4,\n    drive: function() {\n        \/\/ \u591a\u578b\n        Vehicle.drive.call( this );\n        console.log( \"Rolling on all \" + this.wheels + \" wheels!\" );\n    }\n});<\/pre>\n\n\n<p>\u900f\u904e Mixins \u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c07\u5169\u500b\u7269\u4ef6\u6df7\u5408\u6a21\u64ec\u51fa\u7e7c\u627f\u548c\u591a\u578b\u7684\u884c\u70ba\uff0c\u7136\u800c\u5be6\u969b\u4e0a\u5171\u7528\u7684\u51fd\u5f0f\u7269\u4ef6\u4ecd\u53ea\u662f\u4e00\u500b\u53c3\u8003\uff0c\u5982\u679c\u4fee\u6539\u4e86\u51fd\u5f0f\u7269\u4ef6\uff0c\u4f8b\u5982 <code>ignition<\/code>\uff0c<code>Vehicle<\/code> \u548c <code>Car<\/code> \u90fd\u6703\u53d7\u5230\u5f71\u97ff\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AF%84%E7%94%9F%E5%BC%8F%E7%B9%BC%E6%89%BF_Parasitic_Inheritance\"><\/span>\u5bc4\u751f\u5f0f\u7e7c\u627f (Parasitic Inheritance)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>\u660e\u78ba Mixin \u6a21\u5f0f\u7684\u4e00\u7a2e\u8b8a\u9ad4\u3002<\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \"Traditional JS Class\" `Vehicle`\nfunction Vehicle() {\n    this.engines = 1;\n}\nVehicle.prototype.ignition = function() {\n    console.log( \"Turning on my engine.\" );\n};\nVehicle.prototype.drive = function() {\n    this.ignition();\n    console.log( \"Steering and moving forward!\" );\n};\n\/\/ \"Parasitic Class\" `Car`\nfunction Car() {\n    \/\/ first, `car` is a `Vehicle`\n    var car = new Vehicle();\n    \/\/ now, let's modify our `car` to specialize it\n    car.wheels = 4;\n    \/\/ save a privileged reference to `Vehicle::drive()`\n    var vehDrive = car.drive;\n    \/\/ override `Vehicle::drive()`\n    car.drive = function() {\n        vehDrive.call( this );\n        console.log( \"Rolling on all \" + this.wheels + \" wheels!\" );\n    };\n    return car;\n}\nvar myCar = new Car();\nmyCar.drive();\n\/\/ Turning on my engine.\n\/\/ Steering and moving forward!\n\/\/ Rolling on all 4 wheels!<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%9A%B1%E5%90%AB%E7%9A%84_Mixins\"><\/span>\u96b1\u542b\u7684 Mixins<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var Something = {\n    cool: function() {\n        this.greeting = \"Hello World\";\n        this.count = this.count ? this.count + 1 : 1;\n    }\n};\nSomething.cool();\nSomething.greeting; \/\/ \"Hello World\"\nSomething.count; \/\/ 1\nvar Another = {\n    cool: function() {\n    \/\/ implicit mixin of `Something` to `Another`\n        Something.cool.call( this );\n    }\n};\nAnother.cool();\nAnother.greeting; \/\/ \"Hello World\"\nAnother.count; \/\/ 1 (not shared state with `Something`)<\/pre>\n\n\n<p>\u85c9\u7531 <code>Something.cool.call( this );<\/code> \u5229\u7528 this \u5728 <code>Another<\/code> \u4e2d\u6df7\u9032\u4e86 <code>Something<\/code> \u7684\u884c\u70ba\uff0c\u56e0\u70ba\u96b1\u542b\u6307\u5b9a\u7684\u7de3\u6545\uff0c\u5728\u7dad\u8b77\u4e0a\u8b8a\u5f97\u8f03\u4e0d\u6e05\u695a\uff0c\u5efa\u8b70\u76e1\u53ef\u80fd\u907f\u514d\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A4%87%E7%BF%92\"><\/span><strong>\u8907\u7fd2<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<ul class=\"wp-block-list\"><li>\u985e\u5225\u610f\u5473\u8457\u62f7\u8c9d\u3002\u50b3\u7d71\u7684\u985e\u5225\u88ab\u5be6\u9ad4\u5316\u6642\uff0c\u6703\u767c\u751f\u62f7\u8c9d\u884c\u70ba\uff0c\u591a\u578b\u770b\u4f3c\u53c3\u8003\uff0c\u4f46\u5be6\u969b\u4e0a\u4e5f\u662f\u62f7\u8c9d\u3002<\/li><li>\u7136\u800c\uff0cJS \u4e2d\u4e26\u6c92\u6709\u985e\u5225\uff0c\u90fd\u53ea\u662f\u53c3\u8003\u4f86\u5c07\u7269\u4ef6\u9023\u7d50\u5728\u4e00\u8d77\u3002<\/li><li>Mixin \u6a21\u5f0f\u6642\u5e38\u88ab\u7528\u4f86\u6a21\u64ec\u62f7\u8c9d\u884c\u70ba\uff0c\u7136\u800c\u7269\u4ef6\u53ca\u51fd\u5f0f\u4ecd\u53ea\u662f\u8907\u88fd\u5171\u7528\u7684\u53c3\u8003\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u985e\u5225\u7406\u8ad6 OO \u6216\u985e\u5225\u5c0e\u5411\u6240\u5f37\u8abf\u7684\u662f\uff0c\u8cc7\u6599\u672c\u8eab\u5c31\u5177&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[13,12],"tags":[21],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-jsclosuresthis","category-12","tag-javascript"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":0,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}