{"id":191309,"date":"2020-05-25T10:11:28","date_gmt":"2020-05-25T02:11:28","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=191309"},"modified":"2020-05-17T16:12:41","modified_gmt":"2020-05-17T08:12:41","slug":"introduction-to-es6","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/introduction-to-es6.html","title":{"rendered":"ES6 \u6a21\u5757\u7b80\u4ecb"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nES6 \u5f15\u5165\u4e86\u6a21\u5757\u5316\uff0c\u5176\u8bbe\u8ba1\u601d\u60f3\u662f\u5728\u7f16\u8bd1\u65f6\u5c31\u80fd\u786e\u5b9a\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u4ee5\u53ca\u8f93\u5165\u548c\u8f93\u51fa\u7684\u53d8\u91cf\u3002ES6 \u7684\u6a21\u5757\u5316\u5206\u4e3a\u5bfc\u51fa\uff08export\uff09 @\u4e0e\u5bfc\u5165\uff08import\uff09\u4e24\u4e2a\u6a21\u5757\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6982\u8ff0<\/strong><\/div>\n

\u5728 ES6 \u524d\uff0c \u5b9e\u73b0\u6a21\u5757\u5316\u4f7f\u7528\u7684\u662f RequireJS \u6216\u8005 seaJS\uff08\u5206\u522b\u662f\u57fa\u4e8e AMD \u89c4\u8303\u7684\u6a21\u5757\u5316\u5e93\uff0c \u548c\u57fa\u4e8e CMD \u89c4\u8303\u7684\u6a21\u5757\u5316\u5e93\uff09\u3002<\/p>\n

ES6 \u5f15\u5165\u4e86\u6a21\u5757\u5316\uff0c\u5176\u8bbe\u8ba1\u601d\u60f3\u662f\u5728\u7f16\u8bd1\u65f6\u5c31\u80fd\u786e\u5b9a\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u4ee5\u53ca\u8f93\u5165\u548c\u8f93\u51fa\u7684\u53d8\u91cf\u3002<\/p>\n

ES6 \u7684\u6a21\u5757\u5316\u5206\u4e3a\u5bfc\u51fa\uff08export\uff09 @\u4e0e\u5bfc\u5165\uff08import\uff09\u4e24\u4e2a\u6a21\u5757\u3002<\/p>\n

\u7279\u70b9<\/strong><\/div>\n

ES6 \u7684\u6a21\u5757\u81ea\u52a8\u5f00\u542f\u4e25\u683c\u6a21\u5f0f\uff0c\u4e0d\u7ba1\u4f60\u6709\u6ca1\u6709\u5728\u6a21\u5757\u5934\u90e8\u52a0\u4e0a use strict;\u3002<\/p>\n

\u6a21\u5757\u4e2d\u53ef\u4ee5\u5bfc\u5165\u548c\u5bfc\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5982\u51fd\u6570\uff0c\u5bf9\u8c61\uff0c\u5b57\u7b26\u4e32\uff0c\u6570\u5b57\uff0c\u5e03\u5c14\u503c\uff0c\u7c7b\u7b49\u3002<\/p>\n

\u6bcf\u4e2a\u6a21\u5757\u90fd\u6709\u81ea\u5df1\u7684\u4e0a\u4e0b\u6587\uff0c\u6bcf\u4e00\u4e2a\u6a21\u5757\u5185\u58f0\u660e\u7684\u53d8\u91cf\u90fd\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/p>\n

\u6bcf\u4e00\u4e2a\u6a21\u5757\u53ea\u52a0\u8f7d\u4e00\u6b21\uff08\u662f\u5355\u4f8b\u7684\uff09\uff0c \u82e5\u518d\u53bb\u52a0\u8f7d\u540c\u76ee\u5f55\u4e0b\u540c\u6587\u4ef6\uff0c\u76f4\u63a5\u4ece\u5185\u5b58\u4e2d\u8bfb\u53d6\u3002<\/p>\n

export \u4e0e import<\/strong><\/div>\n
\u57fa\u672c\u7528\u6cd5<\/strong><\/span><\/div>\n

\u6a21\u5757\u5bfc\u5165\u5bfc\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5982\u5b57\u7b26\u4e32\uff0c\u6570\u503c\uff0c\u51fd\u6570\uff0c\u7c7b\u3002<\/p>\n

    \n
  1. \u5bfc\u51fa\u7684\u51fd\u6570\u58f0\u660e\u4e0e\u7c7b\u58f0\u660e\u5fc5\u987b\u8981\u6709\u540d\u79f0\uff08export default \u547d\u4ee4\u53e6\u5916\u8003\u8651\uff09\u3002 <\/li>\n
  2. \u4e0d\u4ec5\u80fd\u5bfc\u51fa\u58f0\u660e\u8fd8\u80fd\u5bfc\u51fa\u5f15\u7528\uff08\u4f8b\u5982\u51fd\u6570\uff09\u3002<\/li>\n
  3. export \u547d\u4ee4\u53ef\u4ee5\u51fa\u73b0\u5728\u6a21\u5757\u7684\u4efb\u4f55\u4f4d\u7f6e\uff0c\u4f46\u5fc5\u9700\u5904\u4e8e\u6a21\u5757\u9876\u5c42\u3002<\/li>\n
  4. import \u547d\u4ee4\u4f1a\u63d0\u5347\u5230\u6574\u4e2a\u6a21\u5757\u7684\u5934\u90e8\uff0c\u9996\u5148\u6267\u884c\u3002<\/li>\n<\/ol>\n
    \/*-----export [test.js]-----*\/\r\nlet myName = \"Tom\";\r\nlet myAge = 20;\r\nlet myfn = function(){\r\n    return \"My name is\" + myName + \"! I'm '\" + myAge + \"years old.\"\r\n}\r\nlet myClass =  class myClass {\r\n    static a = \"yeah!\";\r\n}\r\nexport { myName, myAge, myfn, myClass }\r\n \r\n\/*-----import [xxx.js]-----*\/\r\nimport { myName, myAge, myfn, myClass } from \".\/test.js\";\r\nconsole.log(myfn());\/\/ My name is Tom! I'm 20 years old.\r\nconsole.log(myAge);\/\/ 20\r\nconsole.log(myName);\/\/ Tom\r\nconsole.log(myClass.a );\/\/ yeah!<\/pre>\n

    \u5efa\u8bae\u4f7f\u7528\u5927\u62ec\u53f7\u6307\u5b9a\u6240\u8981\u8f93\u51fa\u7684\u4e00\u7ec4\u53d8\u91cf\u5199\u5728\u6587\u6863\u5c3e\u90e8\uff0c\u660e\u786e\u5bfc\u51fa\u7684\u63a5\u53e3\u3002<\/p>\n

    \u51fd\u6570\u4e0e\u7c7b\u90fd\u9700\u8981\u6709\u5bf9\u5e94\u7684\u540d\u79f0\uff0c\u5bfc\u51fa\u6587\u6863\u5c3e\u90e8\u4e5f\u907f\u514d\u4e86\u65e0\u5bf9\u5e94\u540d\u79f0\u3002<\/p>\n

    as \u7684\u7528\u6cd5<\/strong><\/span><\/div>\n

    export \u547d\u4ee4\u5bfc\u51fa\u7684\u63a5\u53e3\u540d\u79f0\uff0c\u987b\u548c\u6a21\u5757\u5185\u90e8\u7684\u53d8\u91cf\u6709\u4e00\u4e00\u5bf9\u5e94\u5173\u7cfb\u3002<\/p>\n

    \u5bfc\u5165\u7684\u53d8\u91cf\u540d\uff0c\u987b\u548c\u5bfc\u51fa\u7684\u63a5\u53e3\u540d\u79f0\u76f8\u540c\uff0c\u5373\u987a\u5e8f\u53ef\u4ee5\u4e0d\u4e00\u81f4\u3002<\/p>\n

    \/*-----export [test.js]-----*\/\r\nlet myName = \"Tom\";\r\nexport { myName as exportName }\r\n \r\n\/*-----import [xxx.js]-----*\/\r\nimport { exportName } from \".\/test.js\";\r\nconsole.log(exportName);\/\/ Tom\r\n\u4f7f\u7528 as \u91cd\u65b0\u5b9a\u4e49\u5bfc\u51fa\u7684\u63a5\u53e3\u540d\u79f0\uff0c\u9690\u85cf\u6a21\u5757\u5185\u90e8\u7684\u53d8\u91cf\r\n\/*-----export [test1.js]-----*\/\r\nlet myName = \"Tom\";\r\nexport { myName }\r\n\/*-----export [test2.js]-----*\/\r\nlet myName = \"Jerry\";\r\nexport { myName }\r\n\/*-----import [xxx.js]-----*\/\r\nimport { myName as name1 } from \".\/test1.js\";\r\nimport { myName as name2 } from \".\/test2.js\";\r\nconsole.log(name1);\/\/ Tom\r\nconsole.log(name2);\/\/ Jerry<\/pre>\n

    \u4e0d\u540c\u6a21\u5757\u5bfc\u51fa\u63a5\u53e3\u540d\u79f0\u547d\u540d\u91cd\u590d\uff0c \u4f7f\u7528 as \u91cd\u65b0\u5b9a\u4e49\u53d8\u91cf\u540d\u3002<\/p>\n

    import \u547d\u4ee4\u7684\u7279\u70b9<\/strong><\/span><\/div>\n

    \u53ea\u8bfb\u5c5e\u6027<\/strong>\uff1a\u4e0d\u5141\u8bb8\u5728\u52a0\u8f7d\u6a21\u5757\u7684\u811a\u672c\u91cc\u9762\uff0c\u6539\u5199\u63a5\u53e3\u7684\u5f15\u7528\u6307\u5411\uff0c\u5373\u53ef\u4ee5\u6539\u5199 import \u53d8\u91cf\u7c7b\u578b\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\uff0c\u4e0d\u80fd\u6539\u5199 import \u53d8\u91cf\u7c7b\u578b\u4e3a\u57fa\u672c\u7c7b\u578b\u7684\u503c\u3002<\/p>\n

    import {a} from \".\/xxx.js\"\r\na = {}; \/\/ error\r\n \r\nimport {a} from \".\/xxx.js\"\r\na.foo = \"hello\"; \/\/ a = { foo : 'hello' }<\/pre>\n

    \u5355\u4f8b\u6a21\u5f0f<\/strong>\uff1a\u591a\u6b21\u91cd\u590d\u6267\u884c\u540c\u4e00\u53e5 import \u8bed\u53e5\uff0c\u90a3\u4e48\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\uff0c\u800c\u4e0d\u4f1a\u6267\u884c\u591a\u6b21\u3002import \u540c\u4e00\u6a21\u5757\uff0c\u58f0\u660e\u4e0d\u540c\u63a5\u53e3\u5f15\u7528\uff0c\u4f1a\u58f0\u660e\u5bf9\u5e94\u53d8\u91cf\uff0c\u4f46\u53ea\u6267\u884c\u4e00\u6b21 import \u3002<\/p>\n

    import { a } \".\/xxx.js\";\r\nimport { a } \".\/xxx.js\";\r\n\/\/ \u76f8\u5f53\u4e8e import { a } \".\/xxx.js\";\r\n \r\nimport { a } from \".\/xxx.js\";\r\nimport { b } from \".\/xxx.js\";\r\n\/\/ \u76f8\u5f53\u4e8e import { a, b } from \".\/xxx.js\";<\/pre>\n

    \u9759\u6001\u6267\u884c\u7279\u6027\uff1aimport \u662f\u9759\u6001\u6267\u884c\uff0c\u6240\u4ee5\u4e0d\u80fd\u4f7f\u7528\u8868\u8fbe\u5f0f\u548c\u53d8\u91cf\u3002<\/p>\n

    import { \"f\" + \"oo\" } from \"methods\";\r\n\/\/ error\r\nlet module = \"methods\";\r\nimport { foo } from module;\r\n\/\/ error\r\nif (true) {\r\n  import { foo } from \"method1\";\r\n} else {\r\n  import { foo } from \"method2\";\r\n}\r\n\/\/ error<\/pre>\n
    export default \u547d\u4ee4<\/strong><\/span><\/div>\n
      \n
    1. \u5728\u4e00\u4e2a\u6587\u4ef6\u6216\u6a21\u5757\u4e2d\uff0cexport\u3001import \u53ef\u4ee5\u6709\u591a\u4e2a\uff0cexport default \u4ec5\u6709\u4e00\u4e2a\u3002<\/li>\n
    2. export default \u4e2d\u7684 default \u662f\u5bf9\u5e94\u7684\u5bfc\u51fa\u63a5\u53e3\u53d8\u91cf\u3002<\/li>\n
    3. \u901a\u8fc7 export \u65b9\u5f0f\u5bfc\u51fa\uff0c\u5728\u5bfc\u5165\u65f6\u8981\u52a0{ }\uff0cexport default \u5219\u4e0d\u9700\u8981\u3002<\/li>\n
    4. export default \u5411\u5916\u66b4\u9732\u7684\u6210\u5458\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u53d8\u91cf\u6765\u63a5\u6536\u3002<\/li>\n<\/ol>\n
      var a = \"My name is Tom!\";\r\nexport default a; \/\/ \u4ec5\u6709\u4e00\u4e2a\r\nexport default var c = \"error\"; \r\n\/\/ error\uff0cdefault \u5df2\u7ecf\u662f\u5bf9\u5e94\u7684\u5bfc\u51fa\u53d8\u91cf\uff0c\u4e0d\u80fd\u8ddf\u7740\u53d8\u91cf\u58f0\u660e\u8bed\u53e5\r\n \r\nimport b from \".\/xxx.js\"; \/\/ \u4e0d\u9700\u8981\u52a0{}\uff0c \u4f7f\u7528\u4efb\u610f\u53d8\u91cf\u63a5\u6536<\/pre>\n
      \u590d\u5408\u4f7f\u7528<\/strong><\/div>\n

      \u6ce8\uff1aimport() \u662f\u63d0\u6848\uff0c\u8fd9\u8fb9\u6682\u65f6\u4e0d\u5ef6\u4f38\u8bb2\u89e3\u3002<\/p>\n

      export \u4e0e import \u53ef\u4ee5\u5728\u540c\u4e00\u6a21\u5757\u4f7f\u7528\uff0c\u4f7f\u7528\u7279\u70b9\uff1a<\/p>\n

        \n
      1. \u53ef\u4ee5\u5c06\u5bfc\u51fa\u63a5\u53e3\u6539\u540d\uff0c\u5305\u62ec default\u3002<\/li>\n
      2. \u590d\u5408\u4f7f\u7528 export \u4e0e import \uff0c\u4e5f\u53ef\u4ee5\u5bfc\u51fa\u5168\u90e8\uff0c\u5f53\u524d\u6a21\u5757\u5bfc\u51fa\u7684\u63a5\u53e3\u4f1a\u8986\u76d6\u7ee7\u627f\u5bfc\u51fa\u7684\u3002<\/li>\n<\/ol>\n
        export { foo, bar } from \"methods\"; \r\n\/\/ \u7ea6\u7b49\u4e8e\u4e0b\u9762\u4e24\u6bb5\u8bed\u53e5\uff0c\u4e0d\u8fc7\u4e0a\u9762\u5bfc\u5165\u5bfc\u51fa\u65b9\u5f0f\u8be5\u6a21\u5757\u6ca1\u6709\u5bfc\u5165 foo \u4e0e bar\r\nimport { foo, bar } from \"methods\";\r\nexport { foo, bar }; \r\n\/* ------- \u7279\u70b9 1 --------*\/\r\n\/\/ \u666e\u901a\u6539\u540d\r\nexport { foo as bar } from \"methods\";\r\n\/\/ \u5c06 foo \u8f6c\u5bfc\u6210 default\r\nexport { foo as default } from \"methods\";\r\n\/\/ \u5c06 default \u8f6c\u5bfc\u6210 foo\r\nexport { default as foo } from \"methods\";\r\n \r\n\/* ------- \u7279\u70b9 2 --------*\/\r\nexport * from \"methods\";<\/pre>\n","protected":false},"excerpt":{"rendered":"

        \u5728 ES6 \u524d\uff0c \u5b9e\u73b0\u6a21\u5757\u5316\u4f7f\u7528\u7684\u662f RequireJS \u6216\u8005 seaJS\uff08\u5206\u522b\u662f\u57fa\u4e8e AMD \u89c4\u8303\u7684\u6a21\u5757\u5316\u5e93 […]<\/p>\n","protected":false},"author":321,"featured_media":191306,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-191309","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\/191309","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\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=191309"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191309\/revisions"}],"predecessor-version":[{"id":191312,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191309\/revisions\/191312"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/191306"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=191309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=191309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=191309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}