{"id":3164,"date":"2018-11-05T09:57:16","date_gmt":"2018-11-05T01:57:16","guid":{"rendered":"http:\/\/www.daliane.com\/?p=3164"},"modified":"2018-11-05T10:00:46","modified_gmt":"2018-11-05T02:00:46","slug":"es6_de_object_assign_xiang_jie","status":"publish","type":"post","link":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/es6_de_object_assign_xiang_jie\/","title":{"rendered":"ES6\u7684Object.assign()\u8be6\u89e3"},"content":{"rendered":"<p>\u6df7\u5165\uff08 Mixin \uff09\u662f\u5728 JS \u4e2d\u7ec4\u5408\u5bf9\u8c61\u65f6\u6700\u6d41\u884c\u7684\u6a21\u5f0f\u3002\u5728\u4e00\u6b21\u6df7\u5165\u4e2d\uff0c\u4e00\u4e2a\u5bf9\u8c61\u4f1a\u4ece\u53e6\u4e00\u4e2a\u5bf9<\/p>\n<p>\n\t\u8c61\u4e2d\u63a5\u6536\u5c5e\u6027\u4e0e\u65b9\u6cd5\u3002\u5f88\u591a JS \u7684\u5e93\u4e2d\u90fd\u6709\u7c7b\u4f3c\u4e0b\u9762\u7684\u6df7\u5165\u65b9\u6cd5\uff1a\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">function mixin(receiver, supplier) {\r\nObject.keys(supplier).forEach(function(key) {\r\nreceiver[key] = supplier[key];\r\n});\r\nreturn receiver;\r\n}<\/pre>\n<p>mixin() \u51fd\u6570\u5728 supplier \u5bf9\u8c61\u7684\u81ea\u6709\u5c5e\u6027\u4e0a\u8fdb\u884c\u8fed\u4ee3\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5c5e\u6027\u590d\u5236\u5230 receiver \u5bf9<br \/>\n\u8c61\uff08\u6d45\u590d\u5236\uff0c\u5f53\u5c5e\u6027\u503c\u4e3a\u5bf9\u8c61\u65f6\uff0c\u4ec5\u590d\u5236\u5176\u5f15\u7528\uff09\u3002\u8fd9\u6837 receiver \u5bf9\u8c61\u5c31\u80fd\u83b7\u5f97\u65b0\u7684\u5c5e\u6027<br \/>\n\u800c\u65e0\u987b\u4f7f\u7528\u7ee7\u627f\uff0c\u6b63\u5982\u4e0b\u9762\u4ee3\u7801\uff1a<\/p>\n<pre class=\"prettyprint lang-js\">function EventTarget() { \/*...*\/ }\r\nEventTarget.prototype = {\r\nconstructor: EventTarget,\r\nemit: function() { \/*...*\/ },\r\non: function() { \/*...*\/ }\r\n};\r\nvar myObject = {};\r\nmixin(myObject, EventTarget.prototype);\r\nmyObject.emit(\"somethingChanged\");<\/pre>\n<p>\u6b64\u5904 myObject \u5bf9\u8c61\u63a5\u6536\u4e86 EventTarget.prototype \u5bf9\u8c61\u7684\u884c\u4e3a\uff0c\u8fd9\u7ed9\u4e86\u5b83\u5206\u522b\u4f7f\u7528 emit()<br \/>\n\u4e0e on() \u65b9\u6cd5\u6765\u53d1\u5e03\u4e8b\u4ef6\u4e0e\u8ba2\u9605\u4e8b\u4ef6\u7684\u80fd\u529b\u3002<br \/>\n\u6b64\u6a21\u5f0f\u5df2\u7ecf\u8db3\u591f\u6d41\u884c\uff0c\u4e8e\u662f ES6 \u5c31\u6dfb\u52a0\u4e86 Object.assign() \u65b9\u6cd5\u6765\u5b8c\u6210\u540c\u6837\u7684\u884c\u4e3a\u3002\u8be5\u65b9\u6cd5<br \/>\n\u63a5\u53d7\u4e00\u4e2a\u63a5\u6536\u8005\uff0c\u4ee5\u53ca\u4efb\u610f\u6570\u91cf\u7684\u4f9b\u5e94\u8005\uff0c\u5e76\u4f1a\u8fd4\u56de\u63a5\u6536\u8005\u3002\u65b9\u6cd5\u540d\u79f0\u4ece mixin() \u53d8\u66f4\u4e3a<br \/>\nassign() \u66f4\u80fd\u53cd\u6620\u51fa\u5b9e\u9645\u53d1\u751f\u7684\u64cd\u4f5c\u3002\u7531\u4e8e mixin() \u51fd\u6570\u4f7f\u7528\u4e86\u8d4b\u503c\u8fd0\u7b97\u7b26\uff08 = \uff09\uff0c\u5b83<br \/>\n\u5c31\u65e0\u6cd5\u5c06\u8bbf\u95ee\u5668\u5c5e\u6027\u590d\u5236\u5230\u63a5\u6536\u8005\u4e0a\uff0c Object.assign() \u4f53\u73b0\u4e86\u8fd9\u79cd\u533a\u522b\u3002<br \/>\n\u5404\u5f0f\u5404\u6837\u7684\u5e93\u4e2d\u90fd\u6709\u76f8\u4f3c\u4f46\u540d\u79f0\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u5176\u57fa\u672c\u529f\u80fd\u76f8\u540c\uff0c\u6d41\u884c\u7684\u66ff\u4ee3\u65b9\u6cd5\u5305\u62ec<br \/>\nextend() \u6216 mix() \u3002\u800c ES6 \u4e5f\u66fe\u5728 Object.assign() \u4e4b\u5916\u77ed\u6682\u5b58\u5728\u4e00\u4e2a<br \/>\nObject.mixin() \u65b9\u6cd5\uff0c\u4e8c\u8005\u7684\u4e3b\u8981\u5dee\u5f02\u5728\u4e8e Object.mixin() \u4e5f\u4f1a\u590d\u5236\u8bbf\u95ee\u5668\u5c5e\u6027\uff0c\u4f46<br \/>\n\u8003\u8651\u5230 super \u7684\u4f7f\u7528\uff08\u8be6\u89c1\u672c\u7ae0\u7684\u201c\u4f7f\u7528 super \u5f15\u7528\u7684\u7b80\u5355\u539f\u578b\u8bbf\u95ee\u201d\u5c0f\u8282\uff09\uff0c\u6b64\u65b9\u6cd5\u6700<br \/>\n\u7ec8\u88ab\u79fb\u9664\u4e86\u3002<\/p>\n<p>\n\t\u4f60\u53ef\u4ee5\u5728\u4efb\u610f\u66fe\u4f7f\u7528 mixin() \u51fd\u6570\u7684\u5730\u65b9\u4f7f\u7528 Object.assign() \uff0c\u6b64\u5904\u6709\u4e2a\u4f8b\u5b50\uff1a\n<\/p>\n<p>\n\t\n<\/p>\n<pre class=\"prettyprint lang-js\">function EventTarget() { \/*...*\/ }\r\nEventTarget.prototype = {\r\nconstructor: EventTarget,\r\nemit: function() { \/*...*\/ },\r\non: function() { \/*...*\/ }\r\n}\r\nvar myObject = {}\r\nObject.assign(myObject, EventTarget.prototype);\r\nmyObject.emit(\"somethingChanged\");<\/pre>\n<p>Object.assign() \u65b9\u6cd5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f9b\u5e94\u8005\uff0c\u800c\u63a5\u6536\u8005\u4f1a\u6309\u7167\u4f9b\u5e94\u8005\u5728\u53c2\u6570\u4e2d\u7684\u987a\u5e8f\u6765\u4f9d\u6b21<br \/>\n\u63a5\u6536\u5b83\u4eec\u7684\u5c5e\u6027\u3002\u8fd9\u610f\u5473\u7740\u5728\u63a5\u6536\u8005\u4e2d\uff0c\u7b2c\u4e8c\u4e2a\u4f9b\u5e94\u8005\u7684\u5c5e\u6027\u53ef\u80fd\u4f1a\u8986\u76d6\u7b2c\u4e00\u4e2a\u4f9b\u5e94\u8005\u7684\uff0c<br \/>\n\u8fd9\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\u5c31\u53d1\u751f\u4e86\uff1a<\/p>\n<p>\n\t\n<\/p>\n<p>\n\tvar receiver = {};<br \/>\nObject.assign(receiver,<br \/>\n{<br \/>\ntype: &#8220;js&#8221;,<br \/>\nname: &#8220;file.js&#8221;<br \/>\n},<br \/>\n{<br \/>\ntype: &#8220;css&#8221;<br \/>\n}<br \/>\n);<br \/>\nconsole.log(receiver.type); \/\/ &#8220;css&#8221;<br \/>\nconsole.log(receiver.name); \/\/ &#8220;file.js&#8221;<br \/>\nreceiver.type \u7684\u503c\u4e3a &#8220;css&#8221; \uff0c\u8fd9\u662f\u56e0\u4e3a\u7b2c\u4e8c\u4e2a\u4f9b\u5e94\u8005\u8986\u76d6\u4e86\u7b2c\u4e00\u4e2a\u4f9b\u5e94\u8005\u7684\u503c\u3002<br \/>\nObject.assign() \u65b9\u6cd5\u5e76\u4e0d\u662f ES6 \u7684\u4e00\u9879\u91cd\u5927\u6269\u5c55\uff0c\u4f46\u5b83\u786e\u5b9e\u5c06\u5f88\u591a JS \u5e93\u4e2d\u7684\u4e00\u4e2a\u516c\u5171\u65b9<br \/>\n\u6cd5\u6807\u51c6\u5316\u4e86\u3002<br \/>\n\u7b2c\u56db\u7ae0 \u6269\u5c55\u7684\u5bf9\u8c61\u529f\u80fd<br \/>\n80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df7\u5165\uff08 Mixin \uff09\u662f\u5728 JS \u4e2d\u7ec4\u5408\u5bf9\u8c61\u65f6\u6700\u6d41\u884c\u7684\u6a21\u5f0f\u3002\u5728\u4e00\u6b21\u6df7\u5165\u4e2d\uff0c\u4e00\u4e2a\u5bf9\u8c61\u4f1a\u4ece\u53e6\u4e00\u4e2a\u5bf9 \u8c61\u4e2d\u63a5\u6536\u5c5e\u6027\u4e0e&#8230;<\/p>\n","protected":false},"author":2,"featured_media":228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[38],"tags":[],"_links":{"self":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3164"}],"collection":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/comments?post=3164"}],"version-history":[{"count":3,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3164\/revisions"}],"predecessor-version":[{"id":3167,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/posts\/3164\/revisions\/3167"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media\/228"}],"wp:attachment":[{"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/media?parent=3164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/categories?post=3164"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.daliane.com\/category\/jinjiaoyou\/wp-json\/wp\/v2\/tags?post=3164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}