{"id":1654,"date":"2017-07-17T17:08:06","date_gmt":"2017-07-17T09:08:06","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1654"},"modified":"2017-07-17T17:08:06","modified_gmt":"2017-07-17T09:08:06","slug":"javascript-symbol-%e6%96%b9%e6%b3%95%e6%b1%87%e6%80%bb","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1654","title":{"rendered":"javascript Symbol \u65b9\u6cd5\u6c47\u603b"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u770b\u5230\u4e00\u5219\u7b11\u8bdd\u3002\u4e3a\u4ec0\u4e48\u4e2d\u56fd\u6700\u8d5a\u94b1\u7684\u4e24\u4e2a\u4e92\u8054\u7f51\u516c\u53f8\uff0c\u4e00\u4e2a\u662f&ldquo;\u4e13\u9a97\u5973\u4eba\u94b1\u7684&rdquo;\uff0c\u4e00\u4e2a\u662f&ldquo;\u4e13\u9a97\u5c0f\u5b69\u5b50\u94b1\u7684&rdquo;\u3002\u5b83\u4eec\u7684\u540d\u5b57\u4e00\u4e2a\u53eb\u963f\u91cc\u5df4\u5df4\uff0c\u4e00\u4e2a\u53eb\u817e\u8baf\u3002\n<\/p>\n<p>\n\t\u53ef\u89c1\u5b66\u597d\u4e92\u8054\u7f51\u6280\u672f\uff0c\u80fd\u4e3a\u8fdb\u6700\u8d5a\u94b1\u7684\u516c\u53f8\u5de5\u4f5c\u6253\u597d\u57fa\u7840\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u7684\u8bb2\u89e3&nbsp;javascript \u4e2d\u65b0\u7684\u6570\u636e\u7c7b\u578b symbol \u7684\u65b9\u6cd5\u7528\u6cd5\u548c\u4f5c\u7528\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"javascript es6\" src=\"http:\/\/es6.ruanyifeng.com\/images\/cover-2nd.jpg\" style=\"width: 800px;height: 1058px\" \/>\n<\/p>\n<h2>\n\tSymbol \u65b9\u6cd5<br \/>\n<\/h2>\n<ul>\n<li>\n\t\tSymbol.for(key)\uff1a\u4f7f\u7528\u7ed9\u5b9a\u7684key\u641c\u7d22\u73b0\u6709\u7b26\u53f7\uff0c\u5982\u679c\u627e\u5230\u5219\u8fd4\u56de\u7b26\u53f7\u3002\u5426\u5219\u5c06\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u4f7f\u7528\u7ed9\u5b9a\u7684key\u5728\u5168\u5c40\u7b26\u53f7\u6ce8\u518c\u8868\u4e2d\u521b\u5efa\u7684\u7b26\u53f7\u3002\n\t<\/li>\n<li>\n\t\tSymbol.keyFor(sym)\uff1a\u4e3a\u7ed9\u5b9a\u7b26\u53f7\u4ece\u5168\u5c40\u7b26\u53f7\u6ce8\u518c\u8868\u4e2d\u68c0\u7d22\u4e00\u4e2a\u5171\u4eab\u7b26\u53f7\u952e\u3002\n\t<\/li>\n<li>\n\t\tSymbol.prototype.toSource()\uff1a\u8fd4\u56de\u5305\u542bSymbol \u5bf9\u8c61\u6e90\u7801\u7684\u5b57\u7b26\u4e32\u3002\u8986\u76d6Object.prototype.toSource() \u65b9\u6cd5\u3002\n\t<\/li>\n<li>\n\t\tSymbol.prototype.toString()\uff1a\u8fd4\u56de\u5305\u542bSymbol\u63cf\u8ff0\u7b26\u7684\u5b57\u7b26\u4e32\u3002 \u8986\u76d6Object.prototype.toString() \u65b9\u6cd5\u3002\n\t<\/li>\n<li>\n\t\tSymbol.prototype.valueOf()\uff1a\u8fd4\u56de Symbol \u5bf9\u8c61\u7684\u521d\u59cb\u503c.\u3002\u8986\u76d6 Object.prototype.valueOf() \u65b9\u6cd5\u3002\n\t<\/li>\n<li>\n\t\tSymbol.prototype[@@toPrimitive]\uff1a\u8fd4\u56deSymbol\u5bf9\u8c61\u7684\u521d\u59cb\u503c\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\t\u5bf9 symbol \u4f7f\u7528 typeof \u8fd0\u7b97\u7b26<br \/>\n<\/h2>\n<p>\n\ttypeof\u8fd0\u7b97\u7b26\u80fd\u5e2e\u52a9\u4f60\u8bc6\u522b\uff08\u6807\u8bc6\uff09symbol\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\ntypeof Symbol() === &#039;symbol&#039;\r\ntypeof Symbol(&#039;foo&#039;) === &#039;symbol&#039;\r\ntypeof Symbol.iterator === &#039;symbol&#039;<\/pre>\n<h2>\n\tSymbol \u7c7b\u578b\u8f6c\u6362<br \/>\n<\/h2>\n<p>\n\t\u5f53\u4f7f\u7528 symbol \u503c\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u4e8b\u60c5\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u5c1d\u8bd5\u5c06\u4e00\u4e2a symbol \u503c\u8f6c\u6362\u4e3a\u4e00\u4e2a number \u503c\u65f6\uff0c\u4f1a\u629b\u51fa\u4e00\u4e2a TypeError \u9519\u8bef &nbsp;(e.g. +sym or sym | 0).\n\t<\/li>\n<li>\n\t\t\u4f7f\u7528\u5bbd\u677e\u76f8\u7b49\u65f6\uff0c Object(sym) == sym returns true.\n\t<\/li>\n<li>\n\t\t\u8fd9\u4f1a\u963b\u6b62\u4f60\u4ece\u4e00\u4e2a symbol \u503c\u9690\u5f0f\u5730\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 string \u7c7b\u578b\u7684\u5c5e\u6027\u540d\u3002\u4f8b\u5982\uff0cSymbol(&quot;foo&quot;) + &quot;bar&quot; \u5c06\u629b\u51fa\u4e00\u4e2a TypeError (can&#039;t convert symbol to string).\n\t<\/li>\n<li>\n\t\t&quot;safer&quot; String(sym) conversion works like a call to Symbol.prototype.toString() with symbols\uff0c\u4f46\u662f\u6ce8\u610f new String(sym) \u5c06\u629b\u51fa\u5f02\u5e38\u3002\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1654\">javascript Symbol \u65b9\u6cd5\u6c47\u603b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u770b\u5230\u4e00\u5219\u7b11\u8bdd\u3002\u4e3a\u4ec0\u4e48\u4e2d\u56fd\u6700\u8d5a\u94b1\u7684\u4e24\u4e2a\u4e92\u8054\u7f51\u516c\u53f8\uff0c\u4e00\u4e2a\u662f&ldquo;\u4e13\u9a97\u5973\u4eba\u94b1\u7684&rdquo;\uff0c\u4e00\u4e2a\u662f&#038;l [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[1379,1106,1378],"class_list":["post-1654","post","type-post","status-publish","format-standard","hentry","category-html5","tag-es6","tag-javascript","tag-symbol"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1654"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1654\/revisions"}],"predecessor-version":[{"id":1655,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1654\/revisions\/1655"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}