{"id":39288,"date":"2023-01-27T17:40:47","date_gmt":"2023-01-27T09:40:47","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=39288"},"modified":"2023-01-27T17:40:47","modified_gmt":"2023-01-27T09:40:47","slug":"java-arrow","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/java-arrow.html","title":{"rendered":"javaES6\u7bad\u5934\u51fd\u6570\u7684\u5168\u65b0\u7279\u6027"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u80d6\u7bad\u5934\u51fd\u6570\uff08Fat arrow functions\uff09\uff0c\u53c8\u79f0\u7bad\u5934\u51fd\u6570\uff0c\u662f\u4e00\u4e2a\u6765\u81eaECMAScript 2015\uff08\u53c8\u79f0ES6\uff09\u7684\u5168\u65b0\u7279\u6027\u3002\u6709\u4f20\u95fb\u8bf4\uff0c\u7bad\u5934\u51fd\u6570\u7684\u8bed\u6cd5\u201c=>\u201d\uff0c\u662f\u53d7\u5230\u4e86CoffeeScript \u7684\u5f71\u54cd\uff0c\u5e76\u4e14\u5b83\u4e0eCoffeeScript\u4e2d\u7684\u201c=>\u201d\u8bed\u6cd5\u4e00\u6837\uff0c\u5171\u4eabthis\u4e0a\u4e0b\u6587\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"123asdghjvcc\"
\n\u7bad\u5934\u51fd\u6570\u7684\u4ea7\u751f\uff0c\u4e3b\u8981\u7531\u4e24\u4e2a\u76ee\u7684\uff1a\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u4e0e\u7236\u4f5c\u7528\u57df\u5171\u4eab\u5173\u952e\u5b57\u201cthis\u201d\u3002\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u6765\u770b\u51e0\u4e2a\u8be6\u7ec6\u7684\u4f8b\u5b50\u3002<\/p>\n

\u65b0\u7684\u51fd\u6570\u8bed\u6cd5<\/strong><\/div>\n

\u4f20\u7edf\u7684\u201cJavaScript\u201d\u51fd\u6570\u8bed\u6cd5\u5e76\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u7684\u7075\u6d3b\u6027\uff0c\u6bcf\u4e00\u6b21\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u4f60\u90fd\u5fc5\u987b\u8f93\u5165\u201cfunction () {}\u201d\u3002\u201cCoffeeScript\u201d\u5982\u4eca\u4e4b\u6240\u4ee5\u90a3\u4e48\u706b\uff0c\u6709\u4e00\u4e2a\u4e0d\u53ef\u5ffd\u7565\u7684\u539f\u56e0\u5c31\u662f\u5b83\u6709\u66f4\u7b80\u6d01\u7684\u51fd\u6570\u8bed\u6cd5\u3002\u66f4\u7b80\u6d01\u7684\u51fd\u6570\u8bed\u6cd5\u5728\u6709\u5927\u91cf\u56de\u8c03\u51fd\u6570\u7684\u573a\u666f\u4e0b\u597d\u5904\u7279\u522b\u660e\u663e\uff0c\u8ba9\u6211\u4eec\u4ece\u4e00\u4e2a\u201cPromise\u201d\u94fe\u7684\u4f8b\u5b50\u770b\u8d77\uff1a<\/p>\n

< class=\"js\">function<\/span> getVerifiedToken<\/span>(selector)<\/span> <\/span>{\r\n  return<\/span> getUsers(selector)\r\n    .then(function<\/span> (users)<\/span> <\/span>{ return<\/span> users[0<\/span>]; })\r\n    .then(verifyUser)\r\n    .then(function<\/span> (user, verifiedToken)<\/span> <\/span>{ return<\/span> verifiedToken; })\r\n    .catch(function<\/span> (err)<\/span> <\/span>{ log(err.stack); });\r\n}<\/pre>\n

\u4ee5\u4e0b\u662f\u4f7f\u7528\u65b0\u7684\u7bad\u5934\u51fd\u6570\u8bed\u6cd5\u8fdb\u884c\u91cd\u6784\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n

< class=\"js\">function<\/span> getVerifiedToken<\/span>(selector)<\/span> <\/span>{\r\n  return<\/span> getUsers(selector)\r\n    .then(users => users[0<\/span>])\r\n    .then(verifyUser)\r\n    .then((user, verifiedToken) => verifiedToken)\r\n    .catch(err => log(err.stack));\r\n}<\/pre>\n

\u4ee5\u4e0b\u662f\u503c\u5f97\u6ce8\u610f\u7684\u51e0\u4e2a\u8981\u70b9\uff1a<\/p>\n

    \n
  • \u201cfunction\u201d\u548c\u201c{}\u201d\u90fd\u6d88\u5931\u4e86\uff0c\u6240\u6709\u7684\u56de\u8c03\u51fd\u6570\u90fd\u53ea\u51fa\u73b0\u5728\u4e86\u4e00\u884c\u91cc\u3002<\/li>\n
  • \u5f53\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u65f6\uff0c\u201c()\u201d\u4e5f\u6d88\u5931\u4e86\uff08rest\u53c2\u6570\u662f\u4e00\u4e2a\u4f8b\u5916\uff0c\u5982\u201c(...args) => ...\u201d\uff09\u3002<\/li>\n
  • \u5f53\"{}\"\u6d88\u5931\u540e\uff0c\u201creturn\u201d\u5173\u952e\u5b57\u4e5f\u8ddf\u7740\u6d88\u5931\u4e86\u3002\u5355\u884c\u7684\u7bad\u5934\u51fd\u6570\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u9690\u5f0f\u7684\u201creturn\u201d\uff08\u8fd9\u6837\u7684\u51fd\u6570\u5728\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5e38\u88ab\u6210\u4e3alamda\u51fd\u6570\uff09\u3002<\/li>\n<\/ul>\n

    \u8fd9\u91cc\u518d\u7740\u91cd\u5f3a\u8c03\u4e00\u4e0b\u4e0a\u8ff0\u7684\u6700\u540e\u4e00\u4e2a\u8981\u6c42\u3002\u4ec5\u4ec5\u5f53\u7bad\u5934\u51fd\u6570\u4e3a\u5355\u884c\u7684\u5f62\u5f0f\u65f6\uff0c\u624d\u4f1a\u51fa\u73b0\u9690\u5f0f\u7684\"return\"\u3002\u5f53\u7bad\u5934\u51fd\u6570\u4f34\u968f\u7740\"{}\"\u88ab\u58f0\u660e\uff0c\u90a3\u4e48\u5373\u4f7f\u5b83\u662f\u5355\u884c\u7684\uff0c\u5b83\u4e5f\u4e0d\u4f1a\u6709\u9690\u5f0f\u201creturn\u201d\uff1a<\/p>\n

    < class=\"js\">const<\/span> getVerifiedToken = selector => {\r\n  return<\/span> getUsers()\r\n    .then(users => users[0<\/span>])\r\n    .then(verifyUser)\r\n    .then((user, verifiedToken) => verifiedToken)\r\n    .catch(err => log(err.stack));\r\n}<\/pre>\n

    \u5982\u679c\u6211\u4eec\u7684\u51fd\u6570\u5185\u53ea\u6709\u4e00\u6761\u58f0\u660e\uff08statement\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u5199\u201c{}\u201d\uff0c\u8fd9\u6837\u770b\u4e0a\u53bb\u4f1a\u548c\u201cCoffeeScript\u201d\u4e2d\u7684\u51fd\u6570\u975e\u5e38\u76f8\u4f3c\uff1a<\/p>\n

    < class=\"js\">const<\/span> getVerifiedToken = selector =>\r\n  getUsers()\r\n    .then(users => users[0<\/span>])\r\n    .then(verifyUser)\r\n    .then((user, verifiedToken) => verifiedToken)\r\n    .catch(err => log(err.stack));<\/pre>\n

    \u4f60\u6ca1\u6709\u770b\u9519\uff0c\u4ee5\u4e0a\u7684\u4f8b\u5b50\u662f\u5b8c\u5168\u5408\u6cd5\u7684ES6\u8bed\u6cd5\u3002\u5f53\u6211\u4eec\u8c08\u8bba\u53ea\u5305\u542b\u4e00\u6761\u58f0\u660e\uff08statement\uff09\u7684\u7bad\u5934\u51fd\u6570\u65f6\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8fd9\u6761\u58f0\u660e\u4e0d\u80fd\u591f\u5206\u6210\u591a\u884c\u5199\u3002<\/p>\n

    \u8fd9\u91cc\u6709\u4e00\u4e2a\u5751\uff0c\u5f53\u5ffd\u7565\u4e86\u201c{}\u201d\u540e\uff0c\u6211\u4eec\u8be5\u600e\u4e48\u8fd4\u56de\u7a7a\u5bf9\u8c61\uff08\u201c{}\u201d\uff09\u5462\uff1f<\/p>\n

    < class=\"js\">const<\/span> emptyObject = () => {};\r\nemptyObject(); \/\/ ?<\/span><\/pre>\n

    \u4e0d\u5e78\u7684\u662f\uff0c\u7a7a\u5bf9\u8c61\u201c{}\u201d\u548c\u7a7a\u767d\u51fd\u6570\u4ee3\u7801\u5757\u201c{}\u201d\u957f\u5f97\u4e00\u6a21\u4e00\u6837\u3002\u3002\u4ee5\u4e0a\u7684\u4f8b\u5b50\u4e2d\uff0c\u201cemptyObject\u201d\u7684\u201c{}\u201d\u4f1a\u88ab\u89e3\u91ca\u4e3a\u4e00\u4e2a\u7a7a\u767d\u51fd\u6570\u4ee3\u7801\u5757\uff0c\u6240\u4ee5\u201cemptyObject()\u201d\u4f1a\u8fd4\u56de\u201cundefined\u201d\u3002\u5982\u679c\u8981\u5728\u7bad\u5934\u51fd\u6570\u4e2d\u660e\u786e\u5730\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61\uff0c\u5219\u4f60\u4e0d\u5f97\u4e0d\u5c06\u201c{}\u201d\u5305\u542b\u5728\u4e00\u5bf9\u5706\u62ec\u53f7\u4e2d(\u201c({})\u201d)\uff1a<\/p>\n

    < class=\"js\">const<\/span> emptyObject = () => ({});\r\nemptyObject(); \/\/ {}<\/span><\/pre>\n

    \u4e0b\u9762\u662f\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u4f8b\u5b50\uff1a<\/p>\n

    < class=\"js\">function<\/span> ()<\/span> <\/span>{ return<\/span> 1<\/span>; }\r\n() => { return<\/span> 1<\/span>; }\r\n() => 1<\/span>\r\n \r\nfunction<\/span> (a)<\/span> <\/span>{ return<\/span> a * 2<\/span>; }\r\n(a) => { return<\/span> a * 2<\/span>; }\r\n(a) => a * 2<\/span>\r\na => a * 2<\/span>\r\n \r\nfunction<\/span> (a, b)<\/span> <\/span>{ return<\/span> a * b; }\r\n(a, b) => { return<\/span> a * b; }\r\n(a, b) => a * b\r\n \r\nfunction<\/span> ()<\/span> <\/span>{ return<\/span> arguments<\/span>[0<\/span>]; }\r\n(...args) => args[0<\/span>]\r\n \r\n() => {} \/\/ undefined<\/span>\r\n() => ({}) \/\/ {}<\/span><\/pre>\n
    this<\/strong><\/div>\n

    \"181531129\"<\/p>\n

    \u201cJavaScript\u201d\u4e2d\u201cthis\u201d\u7684\u6545\u4e8b\u5df2\u7ecf\u662f\u975e\u5e38\u53e4\u8001\u4e86\uff0c\u6bcf\u4e00\u4e2a\u51fd\u6570\u90fd\u6709\u81ea\u5df1\u7684\u4e0a\u4e0b\u6587\u3002\u4ee5\u4e0b\u4f8b\u5b50\u7684\u76ee\u7684\u662f\u4f7f\u7528\u201cjQuery\u201d\u6765\u5c55\u793a\u4e00\u4e2a\u6bcf\u79d2\u90fd\u4f1a\u66f4\u65b0\u7684\u65f6\u949f\uff1a<\/p>\n

    < class=\"js\">$('.current-time'<\/span>).each(function<\/span> ()<\/span> <\/span>{\r\n  setInterval(function<\/span> ()<\/span> <\/span>{\r\n    $(this<\/span>).text(Date<\/span>.now());\r\n  }, 1000<\/span>);\r\n});<\/pre>\n

    \u5f53\u5c1d\u8bd5\u5728\u201csetInterval\u201d\u7684\u56de\u8c03\u4e2d\u4f7f\u7528\u201cthis\u201d\u6765\u5f15\u7528DOM\u5143\u7d20\u65f6\uff0c\u5f88\u4e0d\u5e78\uff0c\u6211\u4eec\u5f97\u5230\u7684\u53ea\u662f\u4e00\u4e2a\u5c5e\u4e8e\u56de\u8c03\u51fd\u6570\u81ea\u8eab\u4e0a\u4e0b\u6587\u7684\u201cthis\u201d\u3002\u4e00\u4e2a\u901a\u5e38\u7684\u89e3\u51b3\u529e\u6cd5\u662f\u5b9a\u4e49\u4e00\u4e2a\u201cthat\u201d\u6216\u8005\u201cself\u201d\u53d8\u91cf\uff1a<\/p>\n

    < class=\"js\">$('.current-time'<\/span>).each(function<\/span> ()<\/span> <\/span>{\r\n  var<\/span> self = this<\/span>;\r\n \r\n  setInterval(function<\/span> ()<\/span> <\/span>{\r\n    $(self).text(Date<\/span>.now());\r\n  }, 1000<\/span>);\r\n});<\/pre>\n

    \u4f46\u5f53\u4f7f\u7528\u80d6\u7bad\u5934\u51fd\u6570\u65f6\uff0c\u8fd9\u4e2a\u95ee\u9898\u5c31\u4e0d\u590d\u5b58\u5728\u4e86\u3002\u56e0\u4e3a\u5b83\u4e0d\u4ea7\u751f\u5c5e\u4e8e\u5b83\u81ea\u5df1\u4e0a\u4e0b\u6587\u7684\u201cthis\u201d\uff1a<\/p>\n

    < class=\"js\">$('.current-time'<\/span>).each(function<\/span> ()<\/span> <\/span>{\r\n  setInterval(() => $(this<\/span>).text(Date<\/span>.now()), 1000<\/span>);\r\n});<\/pre>\n
    arguments\u53d8\u91cf<\/strong><\/div>\n

    \"u22638435572974379852fm21gp0\"
    \n\u7bad\u5934\u51fd\u6570\u4e0e\u666e\u901a\u51fd\u6570\u8fd8\u6709\u4e00\u4e2a\u533a\u522b\u5c31\u662f\uff0c\u5b83\u6ca1\u6709\u81ea\u5df1\u7684\u201carguments\u201d\u53d8\u91cf\uff1a<\/p>\n

    < class=\"js\">function<\/span> log<\/span>(msg)<\/span> <\/span>{\r\n  const<\/span> print = () => console<\/span>.log(arguments<\/span>[0<\/span>]);\r\n  print(`LOG: ${msg}`);\r\n}\r\n \r\nlog('hello'<\/span>); \/\/ hello<\/span><\/pre>\n

    \u518d\u6b21\u91cd\u7533\uff0c\u7bad\u5934\u51fd\u6570\u6ca1\u6709\u5c5e\u4e8e\u81ea\u5df1\u7684\u201cthis\u201d>\u548c\u201carguments\u201d\u3002\u4f46\u662f\uff0c\u4f60\u4ecd\u53ef\u4ee5\u901a\u8fc7rest\u53c2\u6570\uff0c\u6765\u5f97\u5230\u6240\u6709\u4f20\u5165\u7684\u53c2\u6570\u6570\u7ec4\uff1a<\/p>\n

    < class=\"js\">function<\/span> log<\/span>(msg)<\/span> <\/span>{\r\n  const<\/span> print = (...args) => console<\/span>.log(args[0<\/span>]);\r\n  print(`LOG: ${msg}`);\r\n}\r\n \r\nlog('hello'<\/span>); \/\/ LOG: hello<\/span><\/pre>\n
    \u5173\u4e8eyield<\/strong><\/div>\n

    \u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u4f5c\u4e3a\u201cgenerator\u201d\u51fd\u6570\u4f7f\u7528\u3002<\/p>\n

    \u6700\u540e<\/strong><\/div>\n

    \u7bad\u5934\u51fd\u6570\u662f\u6211\u6700\u559c\u6b22\u7684ES6\u7279\u6027\u4e4b\u4e00\u3002\u4f7f\u7528\u201c=>\u201d\u6765\u4ee3\u66ff\u201cfunction\u201d\u662f\u975e\u5e38\u4fbf\u6377\u7684\u3002\u4f46\u6211\u4e5f\u66fe\u89c1\u8fc7\u53ea\u4f7f\u7528\u201c=>\u201d\u6765\u58f0\u660e\u51fd\u6570\u7684\u4ee3\u7801\uff0c\u6211\u5e76\u4e0d\u8ba4\u4e3a\u8fd9\u662f\u597d\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u201c=>\u201d\u4e5f\u63d0\u4f9b\u4e86\u5b83\u533a\u522b\u4e8e\u4f20\u7edf\u201cfunction\u201d\uff0c\u5176\u6240\u72ec\u6709\u7684\u7279\u6027\u3002\u6211\u4e2a\u4eba\u63a8\u8350\uff0c\u4ec5\u5728\u4f60\u9700\u8981\u4f7f\u7528\u5b83\u63d0\u4f9b\u7684\u65b0\u7279\u6027\u65f6\uff0c\u624d\u4f7f\u7528\u5b83\uff1a<\/p>\n