{"id":2686,"date":"2018-04-21T10:42:15","date_gmt":"2018-04-21T02:42:15","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2686"},"modified":"2018-04-21T10:42:15","modified_gmt":"2018-04-21T02:42:15","slug":"symbol-%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2686","title":{"rendered":"Symbol() \u6559\u7a0b"},"content":{"rendered":"<p>\n\tSymbol\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\n<\/p>\n<p>\n\tsymbol\u7684\u4e2d\u6587\u610f\u601d\u662f\uff1a\u7b26\u53f7\uff1b\u8c61\u5f81\uff1b\u6807\u5fd7\uff1b\u8bb0\u53f7\u7b49\u3002\n<\/p>\n<p>\n\t\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&lt;symbol&gt;\u6807\u7b7e\uff0c\u7528\u6765\u6307\u4ee3SVG\u5c0f\u56fe\u6807\u3002\n<\/p>\n<p>\n\t\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\n<\/p>\n<p>\n\t\u8bf4\u5230JavaScript\u539f\u751f\u6570\u636e\u7c7b\u578b\uff0c\u6211\u4eec\u901a\u5e38\u60f3\u5230\u7684\u6709\u8fd96\u79cd\uff1aundefined\u3001null\u3001boolean\u3001string\u3001number\u3001object\u3002\u800csymbol\u662fES6\u65b0\u589e\u7684\u4e00\u4e2a\u539f\u751f\u6570\u636e\u7c7b\u578b\u3002ES6\u5f15\u5165\u4e86\u4e00\u79cd\u65b0\u7684\u539f\u59cb\u6570\u636e\u7c7b\u578bSymbol\uff0c\u8868\u793a\u72ec\u4e00\u65e0\u4e8c\u7684\u503c\u3002\n<\/p>\n<p>\n\tSymbol\u51fd\u6570\u524d\u4e0d\u80fd\u4f7f\u7528new\u547d\u4ee4\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a\u751f\u6210\u7684Symbol\u662f\u4e00\u4e2a\u539f\u59cb\u7c7b\u578b\u7684\u503c\uff0c\u4e0d\u662f\u5bf9\u8c61\u3002\u4f1a\u62a5\u4e0b\u9762\u7684\u9519\u8bef\uff1a\n<\/p>\n<p>\n\tUncaught TypeError: Symbol is not a constructor\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Uncaught TypeError: Symbol is not a constructor\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_205130.png\" style=\"width: 352px;height: 69px\" \/>\n<\/p>\n<p>\n\tSymbol\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u8868\u793a\u5bf9Symbol\u5b9e\u4f8b\u7684\u63cf\u8ff0\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u5728\u63a7\u5236\u53f0\u663e\u793a\uff0c\u6216\u8005\u8f6c\u4e3a\u5b57\u7b26\u4e32\u65f6\uff0c\u6bd4\u8f83\u5bb9\u6613\u533a\u5206\u3002\n<\/p>\n<p>\n\t\u800cSymbol\u672c\u8eab\u53c8\u662f\u4e00\u4e2a\u65b9\u6cd5\u3002\n<\/p>\n<p>\n\t\u4f8b\u5982\u4e0b\u9762JS\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\ntypeof Symbol();<\/pre>\n<p>\n\t\u53ef\u4ee5\u770b\u5230Symbol()\u4f5c\u4e3a\u4e00\u4e2a\u65b9\u6cd5\u6267\u884c\u4e86\uff0c\u540c\u65f6typeof\u5176\u7c7b\u578b\uff0c\u4e5f\u662f&#039;symbol&#039;\uff0c\u5982\u4e0b\u622a\u56fe\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Symbol\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_204332.png\" style=\"width: 143px;height: 42px\" \/>\n<\/p>\n<p>\n\tSymbol\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&mdash;&mdash;\u4f5c\u4e3a\u5bf9\u8c61\u5c5e\u6027\u7684\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u9632\u6b62\u5bf9\u8c61\u5c5e\u6027\u51b2\u7a81\u53d1\u751f\u3002\n<\/p>\n<p>\n\t\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\n<\/p>\n<pre class=\"brush:jscript;\">\r\nlet info1 = {\r\n    name: &#039;\u4e1a\u4f59\u8349&#039;,\r\n    age: 24,\r\n    job: &#039;www.xttblog.com&#039;,\r\n    description: &#039;\u5e73\u65f6\u559c\u6b22\u505a\u505a\u745c\u4f3d\uff0c\u4eba\u5bb6\u6709\u7537\u670b\u53cb\uff0c\u4f60\u522b\u6307\u671b\u4e86&#039;\r\n}\r\nlet info2 = {\r\n    description: &#039;\u8fd9\u5c0f\u59d1\u5a18\u633a\u597d\u7684\uff0c\u633a\u70ed\u60c5\u7684\uff0c\u563f\u563f\u563f&hellip;&hellip;&#039;\r\n}<\/pre>\n<p>\n\t\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\u6027description\uff0c\u4e8e\u662f\u6574\u5408\u7684\u65f6\u5019\uff0c\u5c31\u5bb9\u5668\u51b2\u7a81\uff0c\u8986\u76d6\uff0c\u5bfc\u81f4&ldquo;\u4eba\u5bb6\u6709\u7537\u670b\u53cb&rdquo;\u8fd9\u4e48\u91cd\u8981\u7684\u4fe1\u606f\u90fd\u6ca1\u6ce8\u610f\u5230\u3002\n<\/p>\n<p>\n\t\u4f46\u662f\uff0c\u5982\u679c\u8981\u662fSymbol\uff0c\u5219\u5b8c\u5168\u5c31\u4e0d\u8981\u62c5\u5fc3\u8fd9\u4e2a\u95ee\u9898\u4e86\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nlet info1 = {\r\n    name: &#039;\u5a77\u5a77&#039;,\r\n    age: 24,\r\n    job: &#039;\u516c\u53f8\u524d\u53f0&#039;,\r\n    [Symbol('description')]: &#039;\u5e73\u65f6\u559c\u6b22\u505a\u505a\u745c\u4f3d\uff0c\u4eba\u5bb6\u6709\u7537\u670b\u53cb\uff0c\u4f60\u522b\u6307\u671b\u4e86&#039;\r\n}\r\nlet info2 = {\r\n    [Symbol('description')]: &#039;\u8fd9\u5c0f\u59d1\u5a18\u633a\u597d\u7684\uff0c\u633a\u70ed\u60c5\u7684\uff0c\u563f\u563f\u563f&hellip;&hellip;&#039;\r\n}<\/pre>\n<p>\n\t\u6b64\u65f6\uff0c\u6211\u4eec\u5bf9info1, info2\u5bf9\u8c61\u8fdb\u884c\u590d\u5236\uff0c\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nlet target = {};\r\nObject.assign(target, info1, info2);<\/pre>\n<p>\n\t\u6b64\u65f6target\u5bf9\u8c61\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Symbol\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201804\/2018-04-10_213344.png\" style=\"width: 480px;height: 336px\" \/>\n<\/p>\n<p>\n\t\u59b9\u7eb8\u6240\u6709\u7684\u63cf\u8ff0\u4fe1\u606f\u90fd\u88ab\u5b8c\u5b8c\u6574\u6574\u5730\u4fdd\u7559\u4e86\u4e0b\u6765\u4e86\u3002\n<\/p>\n<p>\n\t\u56e0\u4e3aSymbol()\u8fd4\u56de\u503c\u662f\u552f\u4e00\u7684\uff0c\u4e5f\u5c31\u662f\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nSymbol(&#039;description&#039;) === Symbol(&#039;description&#039;);    \/\/ \u8fd4\u56de\u503c\u662ffalse<\/pre>\n<p>\n\tSymbol\u503c\u4e0d\u80fd\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u503c\u8fdb\u884c\u8fd0\u7b97\n<\/p>\n<pre class=\"brush:jscript;\">\r\n\/\/ \u6ca1\u6709\u53c2\u6570\u7684\u60c5\u51b5\r\nvar s1 = Symbol();\r\nvar s2 = Symbol();\r\n\r\ns1 === s2 \/\/ false\r\n\r\n\/\/ \u6709\u53c2\u6570\u7684\u60c5\u51b5\r\nvar s1 = Symbol(&quot;foo&quot;);\r\nvar s2 = Symbol(&quot;foo&quot;);\r\n\r\ns1 === s2 \/\/ false<\/pre>\n<p>\n\tSymbol\u4f5c\u4e3a\u5c5e\u6027\u4f7f\u7528\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar mySymbol = Symbol();\r\n\r\n\/\/ \u7b2c\u4e00\u79cd\u5199\u6cd5\r\nvar a = {};\r\na[mySymbol] = &#039;Hello!&#039;;\r\n\r\n\/\/ \u7b2c\u4e8c\u79cd\u5199\u6cd5\r\nvar a = {\r\n  [mySymbol]: &#039;Hello!&#039;\r\n};\r\n\r\n\/\/ \u7b2c\u4e09\u79cd\u5199\u6cd5\r\nvar a = {};\r\nObject.defineProperty(a, mySymbol, { value: &#039;Hello!&#039; });\r\n\r\n\/\/ \u4ee5\u4e0a\u5199\u6cd5\u90fd\u5f97\u5230\u540c\u6837\u7ed3\u679c\r\na[mySymbol] \/\/ &quot;Hello!&quot;<\/pre>\n<p>\n\tSymbol\u503c\u4f5c\u4e3a\u5bf9\u8c61\u5c5e\u6027\u540d\u65f6\uff0c\u4e0d\u80fd\u7528\u70b9\u8fd0\u7b97\u7b26\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar a = {};\r\nvar name = Symbol();\r\na.name = &#039;lili&#039;;\r\na[name] = &#039;lucy&#039;;\r\nconsole.log(a.name,a[name]);             \/\/lili,lucy<\/pre>\n<p>\n\tSymbol\u503c\u4f5c\u4e3a\u5c5e\u6027\u540d\u65f6\uff0c\u8be5\u5c5e\u6027\u8fd8\u662f\u516c\u5f00\u5c5e\u6027\uff0c\u4e0d\u662f\u79c1\u6709\u5c5e\u6027\u3002\n<\/p>\n<p>\n\t\u8fd9\u4e2a\u6709\u70b9\u7c7b\u4f3c\u4e8ejava\u4e2d\u7684protected\u5c5e\u6027\uff08protected\u548cprivate\u7684\u533a\u522b\uff1a\u5728\u7c7b\u7684\u5916\u90e8\u90fd\u662f\u4e0d\u53ef\u4ee5\u8bbf\u95ee\u7684\uff0c\u5728\u7c7b\u5185\u7684\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627fprotected\u4e0d\u53ef\u4ee5\u7ee7\u627fprivate\uff09\n<\/p>\n<p>\n\t\u4f46\u662f\u8fd9\u91cc\u7684Symbol\u5728\u7c7b\u5916\u90e8\u4e5f\u662f\u53ef\u4ee5\u8bbf\u95ee\u7684\uff0c\u53ea\u662f\u4e0d\u4f1a\u51fa\u73b0\u5728for&#8230;in\u3001for&#8230;of\u5faa\u73af\u4e2d\uff0c\u4e5f\u4e0d\u4f1a\u88abObject.keys()\u3001Object.getOwnPropertyNames()\u8fd4\u56de\u3002\u4f46\u6709\u4e00\u4e2aObject.getOwnPropertySymbols\u65b9\u6cd5\uff0c\u53ef\u4ee5\u83b7\u53d6\u6307\u5b9a\u5bf9\u8c61\u7684\u6240\u6709Symbol\u5c5e\u6027\u540d\u3002<\/p>\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=2686\">Symbol() \u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<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\u4ee3we [&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,1316,119,1106,1378],"class_list":["post-2686","post","type-post","status-publish","format-standard","hentry","category-html5","tag-es6","tag-filesystem","tag-html5","tag-javascript","tag-symbol"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2686","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=2686"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions"}],"predecessor-version":[{"id":2687,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions\/2687"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}