{"id":1338,"date":"2022-06-29T18:43:43","date_gmt":"2022-06-29T10:43:43","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1338"},"modified":"2022-06-29T23:15:29","modified_gmt":"2022-06-29T15:15:29","slug":"48-javascript-lei-xingsymbol","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1338","title":{"rendered":"48 &#8211; JavaScript \u7c7b\u578b\uff1aSymbol"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16564994267756.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/javascript-types-symbol-52di\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/javascript-types-symbol-52di<\/a><\/p>\n<p>\u5728 JavaScript \u4e2d <code>symbol<\/code> \u662f\u539f\u59cb\u6570\u636e\u7c7b\u578b\u3002<code>Symbol()<\/code> \u51fd\u6570\u8fd4\u56de <code>symbol<\/code> \u7c7b\u578b\u7684\u503c\u4e14\u6c38\u8fdc\u662f\u552f\u4e00\u7684\u3002<code>symbol<\/code> \u88ab\u7528\u6765\u521b\u5efa\u552f\u4e00\u6807\u8bc6\u7b26\uff08\u56e0\u4e3a\u5b83\u4eec\u503c\u7684\u552f\u4e00\u7279\u6027\uff09\uff0c\u591a\u6570\u65f6\u5019\u88ab\u7528\u4e8e\u5bf9\u8c61\u5c5e\u6027\u3002<\/p>\n<h2><a id=\"%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAsymbol\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u521b\u5efa\u4e00\u4e2a symbol<\/h2>\n<pre><code class=\"language-plain_text\">let symbol1 = Symbol();\nlet symbol2 = Symbol(&quot;text&quot;\n<\/code><\/pre>\n<p><code>Symbol()<\/code> \u6ca1\u6709\u6784\u9020\u51fd\u6570\u6240\u4ee5\u4e0d\u652f\u6301 <code>new Symbol()<\/code>\uff0c\u5982\u679c\u4f7f\u7528 new \u5219\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre><code class=\"language-plain_text\">let symbol1 = new Symbol(); \/\/ Symbol is not a constructor\n<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u521b\u5efa <code>symbol<\/code> \u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u65f6\uff0c\u5b83\u5e76\u4e0d\u662f\u628a\u5b57\u7b26\u4e32\u4f5c\u4e3a <code>symbol<\/code>\u3002\u800c\u662f\u6bcf\u6b21\u90fd\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 <code>symbol<\/code> \u3002\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4ec5\u4ec5\u80fd\u4f5c\u4e3a\u4e00\u4e2a\u6807\u7b7e\u5e76\u4e0d\u4ea7\u751f\u4efb\u4f55\u5f71\u54cd\u3002<\/p>\n<pre><code class=\"language-plain_text\">console.log(Symbol(&quot;text&quot;) === Symbol(&quot;text&quot;)); \/\/ false\n<\/code><\/pre>\n<h2><a id=\"symbol%E4%BD%9C%E4%B8%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>symbol \u4f5c\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027<\/h2>\n<p>\u6211\u4eec\u4f7f\u7528\u4e2d\u62ec\u53f7\u6765\u4f7f <code>symbol<\/code> \u4f5c\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<pre><code class=\"language-plain_text\">let userId = Symbol(&quot;id&quot;);\n\nlet employee = {\n    name: &quot;Parwinder&quot;,\n    [userId]: 727\n}\n\nconsole.log(employee[userId]); \/\/ 727\n\n\/\/ above log is not similar to using the string literal &quot;userId&quot;\nconsole.log(employee[&quot;userId&quot;]); \/\/ undefined\n<\/code><\/pre>\n<h2><a id=\"%E9%80%9A%E8%BF%87symbol%E4%B8%BA%E5%AF%B9%E8%B1%A1%E5%88%9B%E5%BB%BA%E9%9A%90%E8%97%8F%E5%B1%9E%E6%80%A7\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u901a\u8fc7 symbol \u4e3a\u5bf9\u8c61\u521b\u5efa&quot;\u9690\u85cf&quot;\u5c5e\u6027<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u4efb\u4f55\u4eba\u90fd\u65e0\u6cd5\u4fee\u6539\u6216\u91cd\u5199\u7684\u201c\u9690\u85cf\u201d\u5c5e\u6027\u3002<\/p>\n<pre><code class=\"language-plain_text\">let car = {\n    name: &quot;BMW&quot;\n};\n\nlet hiddenField = Symbol(&quot;price&quot;);\n\ncar[hiddenField] = 70000;\n\nconsole.log(car); \/\/ { name: 'BMW', [Symbol(price)]: 70000 }\nconsole.log(car[hiddenField]); \/\/ 70000\n<\/code><\/pre>\n<p>\u901a\u8fc7 <code>symbol<\/code> \u4e3a\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u9690\u85cf\u5c5e\u6027\u5728\u4e00\u4e9b\u573a\u666f\u6709\u91cd\u8981\u7684\u4f18\u52bf\u3002<\/p>\n<ol>\n<li>\u8fd9\u4e2a\u9690\u85cf\u7684\u5c5e\u6027\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u6a21\u5757\u3001\u5e93\u6216\u7528\u6237\u4f7f\u7528\u4e00\u4e2a\u770b\u4e0d\u5230 symbol \u5c5e\u6027\u7684 <code>car<\/code> \u5bf9\u8c61\u3002<\/li>\n<li>\u82e5\u5176\u4ed6\u4eba\u60f3\u5728\u5bf9\u8c61\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u9690\u85cf\u5c5e\u6027\uff0c\u56e0\u4e3a symbol \u7684\u552f\u4e00\u6027\u4e0d\u4f1a\u548c\u4f60\u7684\u51b2\u7a81\u3002<\/li>\n<\/ol>\n<p>\u5373\u4f7f symbol \u770b\u4e0a\u53bb\u662f\u5bf9\u8c61\u4e0a\u9690\u85cf\u4e86\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>Object.getOwnPropertySymbols()<\/code> \u83b7\u53d6\u5bf9\u8c61\u4e0a\u7684 symbols\u3002<\/p>\n<pre><code class=\"language-plain_text\">let car = {\n    name: &quot;BMW&quot;\n};\n\nlet hiddenField = Symbol(&quot;price&quot;);\nlet anotherHiddenField = Symbol(&quot;release&quot;);\n\ncar[hiddenField] = 70000;\ncar[anotherHiddenField] = &quot;07\/18\/2020&quot;;\n\nconsole.log(Object.getOwnPropertySymbols(car)); \/\/ [ Symbol(price), Symbol(release) ]\n<\/code><\/pre>\n<h2><a id=\"%E8%AE%B0%E4%BD%8F%EF%BC%9Asymbols%E4%B8%8D%E4%BC%9A%E5%87%BA%E7%8E%B0%E5%9C%A8-for-in%E4%B8%AD\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8bb0\u4f4f\uff1asymbols \u4e0d\u4f1a\u51fa\u73b0\u5728 for&#8230;in \u4e2d<\/h2>\n<pre><code class=\"language-plain_text\">let car = {\n    name: &quot;BMW&quot;,\n    model: &quot;Cooper&quot;,\n    color: &quot;Pearl White&quot;\n};\n\nlet hiddenField = Symbol(&quot;price&quot;);\nlet anotherHiddenField = Symbol(&quot;release&quot;);\n\ncar[hiddenField] = 70000;\ncar[anotherHiddenField] = &quot;07\/18\/2020&quot;;\n\nconsole.log(car[hiddenField]); \/\/ 70000\nconsole.log(car[anotherHiddenField]); \/\/ 07\/18\/2020\n\nfor (let properties in car) {\n    console.log(properties); \/\/ name, model, color\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/javascript-typ&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[89],"class_list":["post-1338","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-symbol"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1338"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1338\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1338\/revisions\/1339"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}