{"id":7473,"date":"2018-04-10T22:37:37","date_gmt":"2018-04-10T14:37:37","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7473"},"modified":"2018-04-11T19:53:58","modified_gmt":"2018-04-11T11:53:58","slug":"known-es6-symbol-function","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/04\/known-es6-symbol-function\/","title":{"rendered":"\u7b80\u5355\u4e86\u89e3ES6\/ES2015 Symbol() \u65b9\u6cd5"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7473\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7473<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/p>\n<h3>\u4e00\u3001\u8def\u8fd8\u5f88\u8fdc\uff0c\u6162\u6162\u8d70\u4e00\u70b9\u4e00\u70b9<\/h3>\n<p>JS\u7684\u5b66\u4e60\u505c\u6ede\u4e86\u5f88\u4e45\uff0c\u5f53\u60f3\u8981\u91cd\u65b0\u62fe\u8d77\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u5df2\u7ecf\u62c9\u4e86\u5f88\u591a\u4e1c\u897f\u4e86\u3002<\/p>\n<p>\u7136\u800c\uff0c\u81ea\u5df1\u5e76\u4e0d\u7740\u6025\u3002\u671d\u82b1\u56e0\u6545\u672a\u91c7\u64b7\uff0c\u5915\u62fe\u4f9d\u65e7\u70c2\u6f2b\u5929\u3002\u4ece\u5c0f\u5904\u5f00\u59cb\uff0c\u4e00\u70b9\u4e00\u70b9\u4e0a\u4e0b\u6c42\u7d22\uff0c\u6f2b\u6f2b\u957f\u8def\u5c31\u4f1a\u5168\u7136\u8d70\u5728\u811a\u4e0b\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4eceES6\u4e00\u4e2a\u540d\u4e0d\u89c1\u7ecf\u4f20\u7684<code>Symbol()<\/code>\u65b9\u6cd5\u8bf4\u8d77\u3002<\/p>\n<p><span class=\"s\">\/\/zxx: \u672c\u6587\u90e8\u5206\u5185\u5bb9\u53c2\u8003\u81ea<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Symbol\" rel=\"noopener\" target=\"_blank\">MDN\u4e0a\u5173\u4e8eSymbol<\/a>\u7684\u6587\u6863\u3002<\/span><\/p>\n<h3>\u4e8c\u3001\u5173\u4e8esymbol\u8fd9\u4e2a\u8bcd<\/h3>\n<p>Symbol\u8fd9\u4e2a\u8bcd\u5728IT\u8f6f\u4ef6\u9886\u57df\u5b9e\u9645\u4e0a\u662f\u4e2a\u5e38\u89c1\u89d2\u8272\uff0c\u5728\u4f20\u7edf\u524d\u7aef\u6280\u672f\u9886\u57df\uff0c\u8fd9\u4e2a\u8bcd\u51fa\u73b0\u9891\u7387\u6709\u9650\uff0c\u4f46\u662f\uff0c\u968f\u7740\u73b0\u4ee3web\u6280\u672f\u7684\u53d1\u5c55\uff0cSymbol\u8fd9\u4e2a\u8bcd\u5f00\u59cb\u5728\u4e0d\u540c\u524d\u7aef\u8bed\u8a00\u4e2d\u51fa\u73b0\u3002<\/p>\n<p>symbol\u7684\u4e2d\u6587\u610f\u601d\u662f\uff1a\u7b26\u53f7\uff1b\u8c61\u5f81\uff1b\u6807\u5fd7\uff1b\u8bb0\u53f7\u7b49\u3002<\/p>\n<p>\u6211\u6700\u65e9\u77e5\u9053\u662f\u5728Adobe Illustrator\u8f6f\u4ef6\u4e2d\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u77e2\u91cf\u7b26\u53f7\u5b58\u5728\u3002\u5728SVG\u4e2d\uff0c\u5c31\u6709\u4e13\u95e8\u7684<code>&lt;symbol&gt;<\/code>\u6807\u7b7e\uff0c\u7528\u6765\u6307\u4ee3SVG\u5c0f\u56fe\u6807\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\uff0cSymbol\u53c8\u6709\u4e86\u53e6\u5916\u7684\u89d2\u8272\uff0c\u4f5c\u4e3a\u4e00\u4e2aJavaScript\u7684\u539f\u751f\u6570\u636e\u7c7b\u578b\uff08primitive data type\uff09\u5b58\u5728\u3002<\/p>\n<p>\u8bf4\u5230JavaScript\u539f\u751f\u6570\u636e\u7c7b\u578b\uff0c\u6211\u4eec\u901a\u5e38\u60f3\u5230\u7684\u6709\u8fd96\u79cd\uff1a<code>undefined<\/code>\u3001<code>null<\/code>\u3001<code>boolean<\/code>\u3001<code>string<\/code>\u3001<code>number<\/code>\u3001<code>object<\/code>\u3002\u800c<code>symbol<\/code>\u662fES6\u65b0\u589e\u7684\u4e00\u4e2a\u539f\u751f\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p>\u800cSymbol\u672c\u8eab\u53c8\u662f\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762JS\uff1a<\/p>\n<pre>typeof Symbol();<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230<code>Symbol()<\/code>\u4f5c\u4e3a\u4e00\u4e2a\u65b9\u6cd5\u6267\u884c\u4e86\uff0c\u540c\u65f6<code>typeof<\/code>\u5176\u7c7b\u578b\uff0c\u4e5f\u662f<code>'symbol'<\/code>\uff0c\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_204332.png\" width=\"143\" height=\"42\" alt=\"symbol\u7c7b\u578b\u663e\u793a\" class=\"alignnone\" \/><\/p>\n<p>\u4f46<strong>Symbol\u4e0d\u80fd\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u4f7f\u7528<\/strong>\uff0c\u4e5f\u5c31\u662f\u4e0b\u9762\u8fd9\u6837\u662f\u4f1a\u55dd\u5c41\u7684\uff01<\/p>\n<pre>new Symbol();<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_205130.png\" width=\"352\" height=\"69\" alt=\"\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u5931\u8d25\" class=\"alignnone\" \/><\/p>\n<h3>\u4e09\u3001Symbol\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/h3>\n<p>Symbol\u7684\u4f5c\u7528\u975e\u5e38\u7684\u4e13\u4e00\uff0c\u6362\u53e5\u8bdd\u8bf4\u5176\u8bbe\u8ba1\u51fa\u6765\u5c31\u53ea\u6709\u4e00\u4e2a\u76ee\u7684\u2014\u2014<strong>\u4f5c\u4e3a\u5bf9\u8c61\u5c5e\u6027\u7684\u552f\u4e00\u6807\u8bc6\u7b26<\/strong>\uff0c\u9632\u6b62\u5bf9\u8c61\u5c5e\u6027\u51b2\u7a81\u53d1\u751f\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f60\u770b\u4e0a\u4e86\u516c\u53f8\u524d\u6765\u7684\u524d\u53f0\u59b9\u7eb8\uff0c\u60f3\u4e86\u89e3\u5173\u4e8e\u5979\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u4e8e\u662f\u5c31\u8be2\u95eeHr\u540c\u4e8b\uff0c\u626b\u5730\u963f\u59e8\uff0c\u4e8e\u662f\u5f97\u5230\u7c7b\u4f3c\u8fd9\u6837\u4fe1\u606f\uff1a<\/p>\n<pre>let info1 = {\n    name: '\u5a77\u5a77',\n    age: 24,\n    job: '\u516c\u53f8\u524d\u53f0',\n    description: '\u5e73\u65f6\u559c\u6b22\u505a\u505a\u745c\u4f3d\uff0c\u4eba\u5bb6\u6709\u7537\u670b\u53cb\uff0c\u4f60\u522b\u6307\u671b\u4e86'\n}\nlet info2 = {\n    description: '\u8fd9\u5c0f\u59d1\u5a18\u633a\u597d\u7684\uff0c\u633a\u70ed\u60c5\u7684\uff0c\u563f\u563f\u563f\u2026\u2026'\n}<\/pre>\n<p>\u663e\u7136\uff0c\u4f60\u9700\u8981\u5bf9\u8fd9\u4e24\u4e2a\u6570\u636e\u8fdb\u884c\u6c47\u603b\uff0c\u7ed3\u679c\uff0c\u5c31\u4f1a\u53d1\u73b0\uff0c\u63cf\u8ff0\u90fd\u7528\u4e86\u540c\u4e00\u4e2a\u5bf9\u8c61\u5c5e\u6027<code>description<\/code>\uff0c\u4e8e\u662f\u6574\u5408\u7684\u65f6\u5019\uff0c\u5c31\u5bb9\u5668\u51b2\u7a81\uff0c\u8986\u76d6\uff0c\u5bfc\u81f4\u201c\u4eba\u5bb6\u6709\u7537\u670b\u53cb\u201d\u8fd9\u4e48\u91cd\u8981\u7684\u4fe1\u606f\u90fd\u6ca1\u6ce8\u610f\u5230\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u8981\u662fSymbol\uff0c\u5219\u5b8c\u5168\u5c31\u4e0d\u8981\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\u4e86\uff1a<\/p>\n<pre>let info1 = {\n    name: '\u5a77\u5a77',\n    age: 24,\n    job: '\u516c\u53f8\u524d\u53f0',\n    [Symbol('description')]: '\u5e73\u65f6\u559c\u6b22\u505a\u505a\u745c\u4f3d\uff0c\u4eba\u5bb6\u6709\u7537\u670b\u53cb\uff0c\u4f60\u522b\u6307\u671b\u4e86'\n}\nlet info2 = {\n    [Symbol('description')]: '\u8fd9\u5c0f\u59d1\u5a18\u633a\u597d\u7684\uff0c\u633a\u70ed\u60c5\u7684\uff0c\u563f\u563f\u563f\u2026\u2026'\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u5bf9info1, info2\u5bf9\u8c61\u8fdb\u884c\u590d\u5236\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>let target = {};\nObject.assign(target, info1, info2);<\/pre>\n<p>\u6b64\u65f6<code>target<\/code>\u5bf9\u8c61\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_213344.png\" width=\"480\" height=\"336\" alt=\"target\u5bf9\u8c61\u8f93\u51fa\u622a\u56fe\" class=\"alignnone\" \/><\/p>\n<p>\u59b9\u7eb8\u6240\u6709\u7684\u63cf\u8ff0\u4fe1\u606f\u90fd\u88ab\u5b8c\u5b8c\u6574\u6574\u5730\u4fdd\u7559\u4e86\u4e0b\u6765\u4e86\u3002<\/p>\n<p>\u56e0\u4e3a<code>Symbol()<\/code>\u8fd4\u56de\u503c\u662f\u552f\u4e00\u7684\uff0c\u4e5f\u5c31\u662f\uff1a<\/p>\n<pre>Symbol('description') === Symbol('description');    <span style=\"color:green;\">\/\/ \u8fd4\u56de\u503c\u662ffalse<\/span><\/pre>\n<h3>\u56db\u3001Symbol()\u7684\u8bed\u6cd5<\/h3>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>Symbol([description])<\/pre>\n<p>\u5176\u4e2d<code>description<\/code>\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u5b57\u7b26\u4e32\uff0c\u6ca1\u4ec0\u4e48\u7279\u522b\u7684\u4f5c\u7528\uff0c\u5c31\u662fdebug\u8c03\u8bd5\u7684\u65f6\u5019\u53ef\u4ee5\u7528\u6765\u4f5c\u4e3a\u6807\u8bb0\u3002<\/p>\n<h4>\u5982\u4f55\u83b7\u53d6Symbol()\u5bf9\u5e94\u5c5e\u6027\u503c\uff1f<\/h4>\n<p>\u62ff\u4e0a\u9762<code>target<\/code>\u4e3e\u4f8b\uff0c\u5982\u4f55\u83b7\u5f97\u5bf9\u59b9\u7eb8\u7684<code>description<\/code>\u63cf\u8ff0\u4fe1\u606f\u5462\uff1f<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>Object.getOwnPropertySymbols(obj)<\/code>\u8fd9\u4e2a\u65b9\u6cd5\u8fdb\u884c\u83b7\u53d6\uff0c\u53ef\u4ee5\u8fd4\u56de<code>obj<\/code>\u5bf9\u8c61\u4e2d\u7684Symbol\u4fe1\u606f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>Object.getOwnPropertySymbols(target);<\/pre>\n<p>\u59b9\u7eb8\u7684\u63cf\u8ff0\u4fe1\u606f\u5c31\u51fa\u73b0\u4e86\uff0c\u5982\u4e0b\u622a\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_214655.png\" width=\"371\" height=\"106\" alt=\"\u59b9\u7eb8\u7684\u63cf\u8ff0\u4fe1\u606f\" class=\"alignnone\" \/><\/p>\n<p>Symbol\u5728\u548c\u5bf9\u8c61\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u5f80\u5f80\u79bb\u4e0d\u5f00JS\u4e2d\u7684\u6570\u7ec4\u62ec\u53f7<code>[]<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>var smy = Symbol();\nvar info = {\n  smy: 'x',\n  [smy]: 'y'\n};\n<\/pre>\n<p>\u6b64\u65f6\uff1a<\/p>\n<pre>console.log(info.smy);       <span style=\"color:green;\">\/\/ \u8f93\u51fa'x'<\/span>\nconsole.log(info['smy']);    <span style=\"color:green;\">\/\/ \u8f93\u51fa'x'<\/span>\nconsole.log(info[smy]);      <span style=\"color:green;\">\/\/ \u8f93\u51fa'y'<\/span><\/pre>\n<h3>\u4e94\u3001Symbol\u5176\u4ed6\u4e00\u4e9b\u77e5\u8bc6<\/h3>\n<h4>1. Symbol\u4e0e\u8fd0\u7b97\u3001\u7c7b\u578b\u8f6c\u6362\u7b49<\/h4>\n<p><code>symbol<\/code>\u503c\u867d\u7136\u4e0d\u662f\u5bf9\u8c61\uff0c\u4f46\u662f\u6839\u636e\u81ea\u5df1\u6d4b\u8bd5\uff0c\u5728Chrome\u548cFirefox\u4e0b\u90fd\u662f\u53ef\u4ee5\u6dfb\u52a0\u5c5e\u6027\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>var smy = Symbol();\nsmy.description = '\u63cf\u8ff0';<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_221313.png\" width=\"205\" height=\"53\" alt=\"\u6dfb\u52a0\u5c5e\u6027\" class=\"alignnone\" \/><\/p>\n<p><strong>\u66f4\u65b0\u4e8e\u7fcc\u65e5<\/strong><br \/>\n\u611f\u8c22yyhaxx\u7684\u53cd\u9988\uff0c\u5728macos chrome 65.0.3325.181\u548cnode v8.9.4\u4e2d\u6d4b\u8bd5\u7ed9symbol\u52a0\u5c5e\u6027\u7ed3\u679c\u662f<code>undefined<\/code>\uff0c\u4e0e\u4e0a\u9762window PC\u6d4b\u8bd5\u7ed3\u679c\u6709\u4e9b\u7ec6\u8282\u5dee\u5f02\u3002<\/p>\n<p>\u4f46\u662f\u5728\u7c7b\u578b\u8f6c\u6362\u65f6\u5019<code>symbol<\/code>\u503c\u4f1a\u9047\u5230\u4e0d\u5c11\u5c40\u9650\uff1a<\/p>\n<ul>\n<li>Symbol\u503c\u53ef\u4ee5\u663e\u5f0f\u8f6c\u4e3a\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u8f6c\u4e3a\u5e03\u5c14\u503c\uff0c\u4f46\u662f\u4e0d\u80fd\u8f6c\u4e3a\u6570\u503c\u3002\u4f8b\u5982\uff1a<code>+sym<\/code>\u4f1a\u62a5\u9519\uff0c\u5982\u4e0b\u56fe\uff1a\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_221713.png\" width=\"451\" height=\"53\" alt=\"\u8f6c\u6362\u6570\u503c\u51fa\u9519\" class=\"alignnone\" \/><\/p>\n<p>\u9690\u5f0f\u5730\u521b\u5efa\u4e00\u4e2a\u65b0\u7684<code>string<\/code>\u7c7b\u578b\u7684\u5c5e\u6027\u540d\u4e5f\u4f1a\u62a5\u9519\uff0c\u4f8b\u5982<code>Symbol(\"foo\") + \"bar\"<\/code> \u5c06\u629b\u51fa\u4e00\u4e2aTypeError\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_222004.png\" width=\"443\" height=\"50\" alt=\"\u533f\u540d\u5b57\u7b26\u8f6c\u6362\u62a5\u9519\" class=\"alignnone\" \/>\n<\/li>\n<li>\u4f7f\u7528\u5bbd\u677e\u76f8\u7b49\u65f6\uff0c <code>Object(sym) == sym<\/code>\u8fd4\u56de\u503c\u662f<code>true<\/code>\u3002\u6ce8\u610f\u8fd9\u91cc\u5916\u9762\u5957\u7684\u662f<code>Object()<\/code>\uff1b<\/li>\n<\/ul>\n<h4>2. Symbol\u4e0efor&#8230;in\u8fed\u4ee3<\/h4>\n<p>Symbols\u5728<code>for...in<\/code>\u8fed\u4ee3\u4e2d\u4e0d\u53ef\u679a\u4e3e\uff0c\u5982\u679c\u60f3\u8981\u8fbe\u5230\u6548\u679c\uff0c\u501f\u52a9<code>Object.getOwnPropertySymbols(obj)<\/code>\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<pre>var obj = {};\n\nobj[Symbol(\"a\")] = \"a\";\nobj[Symbol.for(\"b\")] = \"b\";\nobj[\"c\"] = \"c\";\nobj.d = \"d\";\n\nfor (var i in obj) {\n   console.log(i);   <span style=\"color:green;\">\/\/ \u8f93\u51fa \"c\" \u548c \"d\"<\/span>\n}<\/pre>\n<h4>3. Symbol\u4e0eJSON.stringify()<\/h4>\n<p>\u5f53\u4f7f\u7528<code>JSON.strIngify()<\/code>\u65f6\u4ee5<code>symbol<\/code>\u503c\u4f5c\u4e3a\u952e\u7684\u5c5e\u6027\u4f1a\u88ab\u5b8c\u5168\u5ffd\u7565\uff0c\u793a\u610f\u4ee3\u7801\uff1a<\/p>\n<pre>\nJSON.stringify({[Symbol(\"foo\")]: \"foo\"});    <span style=\"color:green;\">\/\/ '{}'<\/span>              \n<\/pre>\n<h4>4. Symbol\u5305\u88c5\u5668\u5bf9\u8c61\u4f5c\u4e3a\u5c5e\u6027\u7684\u952e<\/h4>\n<p>\u56f4\u7ed5\u539f\u59cb\u6570\u636e\u7c7b\u578b\u521b\u5efa\u4e00\u4e2a\u663e\u5f0f\u5305\u88c5\u5668\u5bf9\u8c61\u4eceECMAScript 6\u5f00\u59cb\u4e0d\u518d\u88ab\u652f\u6301\uff0c\u6240\u4ee5<code>new Symbol()<\/code>\u4f1a\u62a5\u9519\uff0c\u7136\u800c\uff0c\u73b0\u6709\u7684\u539f\u59cb\u5305\u88c5\u5668\u5bf9\u8c61\uff0c\u5982 <code>new Boolean<\/code>\u3001<code>new String<\/code>\u4ee5\u53ca<code>new Number<\/code>\u56e0\u4e3a\u9057\u7559\u539f\u56e0\u4ecd\u53ef\u88ab\u521b\u5efa\u3002<\/p>\n<p>\u6b64\u65f6\uff0c\u5982\u679c\u6211\u4eec\u60f3\u521b\u5efa\u4e00\u4e2a<code>Symbol<\/code>\u5305\u88c5\u5668\u5bf9\u8c61 (Symbol wrapper object)\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Object()<\/code>\u51fd\u6570\uff1a<\/p>\n<pre>var sym = Symbol(\"foo\");\ntypeof sym;     <span style=\"color:green;\">\/\/ \"symbol\"<\/span>\nvar symObj = Object(sym);\ntypeof symObj;  <span style=\"color:green;\">\/\/ \"object\"<\/span><\/pre>\n<p>\u5f53\u4e00\u4e2a<code>Symbol<\/code>\u5305\u88c5\u5668\u5bf9\u8c61\u4f5c\u4e3a\u4e00\u4e2a\u5c5e\u6027\u7684\u952e\u65f6\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5c06\u88ab\u5f3a\u5236\u8f6c\u6362\u4e3a\u5b83\u5305\u88c5\u8fc7\u7684<code>symbol<\/code>\u503c\uff1a<\/p>\n<pre>var sym = Symbol(\"foo\");\nvar obj = {[sym]: 1};\nobj[sym];            <span style=\"color:green;\">\/\/ 1<\/span>\nobj[Object(sym)];    <span style=\"color:green;\">\/\/ \u8fd8\u662f1<\/span><\/pre>\n<h3>\u516d\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>\u5b9e\u7528\u89d2\u5ea6\u8bb2\uff0c\u4e0a\u9762\u5173\u4e8eSymbol()\u7684\u77e5\u8bc6\u5df2\u7ecf\u8db3\u591f\u4e86\uff0c\u5b9e\u9645\u4e0a\u6587\u6863\u4e2d\u867d\u7136\u8fd8\u5c55\u793a\u4e86\u5f88\u591a\u5176\u4ed6API\u548c\u65b9\u6cd5\uff0c\u4f8b\u5982Symbol\u5728\u5168\u5c40\u6709\u4e2a\u6ce8\u518c\u8868\uff0c\u5b83\u4e3a\u5b57\u7b26\u4e32\u548cSymbol\u63d0\u4f9b\u4e86\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u8fd9\u79cd\u5bf9\u5e94\u5173\u7cfb\u751a\u81f3\u662f\u8de8service worker\u548ciframe\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>Symbol.for(key)<\/code>\u8fd4\u56de<code>Symbol<\/code>\u3002<\/p>\n<p>\u4e0d\u8fc7\u6587\u4e2d\u5e76\u6ca1\u6709\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u56e0\u4e3a\u672c\u6587\u6807\u9898\u662f\u201c\u7b80\u5355\u4ecb\u7ecd\u201d\uff0c\u54c8\u54c8\uff0c\u5f00\u4e2a\u73a9\u7b11\uff0c\u5b9e\u9645\u4e0a\u8003\u8651\u7684\u662f\u6295\u8d44\u6536\u76ca\u6bd4\uff0c\u5982\u679c\u6211\u4eec\u8ffd\u6c42\u7684\u4e0d\u662f\u6210\u4e3a\u5728\u5f88\u9ad8\u6280\u672f\u9020\u8be3\u7684\u725b\u4eba\uff0c\u5f53\u4e0b\u8fd9\u4e2a\u9636\u6bb5\uff0c\u5176\u5b9e\u53ef\u4ee5\u4e0d\u5fc5\u82b1\u592a\u591a\u65f6\u95f4\u8fc7\u4e8e\u6df1\u7a76\uff0c\u9762\u5411\u4e1a\u52a1\u9762\u5411\u9879\u76ee\uff0c\u6536\u76ca\u8981\u66f4\u5927\u4e9b\u3002<\/p>\n<p>\u597d\uff0c\u5c31\u8fd9\u4e9b\uff0c\u771f\u00b7\u7b80\u5355\u4ecb\u7ecd\u3002<\/p>\n<p>ECMAScript 6\u76f8\u5173\u77e5\u8bc6\u6211\u81ea\u5df1\u4e5f\u5904\u4e8e\u5a74\u513f\u5b66\u6b65\u9636\u6bb5\uff0c\u5982\u679c\u6709\u8868\u8ff0\u4e0d\u51c6\u786e\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u5927\u529b\u6307\u6b63\uff01<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/14.png\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=5474\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=5474<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Symbol\u662fJavaScript ES6\u65b0\u51fa\u73b0\u7684\u4e00\u4e2a\u7684\u539f\u751f\u6570\u636e\u7c7b\u578b\uff08primitive data type\uff09\uff0c\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f\u6709\u4ec0\u4e48\u7279\u6027\uff1f\u672c\u6587\u5c31\u5c06\u7b80\u5355\u4ecb\u7ecd\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u80fd\u591f\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[904,1236,553,1237,1235,930],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7473"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=7473"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7473\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=7473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=7473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=7473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}