\u7528\u6cd5<\/strong><\/p>\n\u7531\u4e8e\u6bcf\u4e00\u4e2a Symbol \u7684\u503c\u90fd\u662f\u4e0d\u76f8\u7b49\u7684\uff0c\u6240\u4ee5 Symbol \u4f5c\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u5c5e\u6027\u4e0d\u91cd\u540d\u3002<\/p>\n
let sy = Symbol(\"key1\");\r\n \r\n\/\/ \u5199\u6cd51\r\nlet syObject = {};\r\nsyObject[sy] = \"kk\";\r\nconsole.log(syObject); \/\/ {Symbol(key1): \"kk\"}\r\n \r\n\/\/ \u5199\u6cd52\r\nlet syObject = {\r\n [sy]: \"kk\"\r\n};\r\nconsole.log(syObject); \/\/ {Symbol(key1): \"kk\"}\r\n \r\n\/\/ \u5199\u6cd53\r\nlet syObject = {};\r\nObject.defineProperty(syObject, sy, {value: \"kk\"});\r\nconsole.log(syObject); \/\/ {Symbol(key1): \"kk\"}<\/pre>\nSymbol \u4f5c\u4e3a\u5bf9\u8c61\u5c5e\u6027\u540d\u65f6\u4e0d\u80fd\u7528.\u8fd0\u7b97\u7b26\uff0c\u8981\u7528\u65b9\u62ec\u53f7\u3002\u56e0\u4e3a.\u8fd0\u7b97\u7b26\u540e\u9762\u662f\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u53d6\u5230\u7684\u662f\u5b57\u7b26\u4e32 sy \u5c5e\u6027\uff0c\u800c\u4e0d\u662f Symbol \u503c sy \u5c5e\u6027\u3002<\/p>\n
let syObject = {};\r\nsyObject[sy] = \"kk\";\r\n \r\nsyObject[sy]; \/\/ \"kk\"\r\nsyObject.sy; \/\/ undefined<\/pre>\n\u6ce8\u610f\u70b9<\/strong><\/span><\/div>\nSymbol \u503c\u4f5c\u4e3a\u5c5e\u6027\u540d\u65f6\uff0c\u8be5\u5c5e\u6027\u662f\u516c\u6709\u5c5e\u6027\u4e0d\u662f\u79c1\u6709\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u3002\u4f46\u662f\u4e0d\u4f1a\u51fa\u73b0\u5728 for...in \u3001 for...of \u7684\u5faa\u73af\u4e2d\uff0c\u4e5f\u4e0d\u4f1a\u88ab Object.keys() \u3001 Object.getOwnPropertyNames() \u8fd4\u56de\u3002\u5982\u679c\u8981\u8bfb\u53d6\u5230\u4e00\u4e2a\u5bf9\u8c61\u7684 Symbol \u5c5e\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7 Object.getOwnPropertySymbols() \u548c Reflect.ownKeys() \u53d6\u5230\u3002<\/p>\n
let syObject = {};\r\nsyObject[sy] = \"kk\";\r\nconsole.log(syObject);\r\n \r\nfor (let i in syObject) {\r\n console.log(i);\r\n} \/\/ \u65e0\u8f93\u51fa\r\n \r\nObject.keys(syObject); \/\/ []\r\nObject.getOwnPropertySymbols(syObject); \/\/ [Symbol(key1)]\r\nReflect.ownKeys(syObject); \/\/ [Symbol(key1)]<\/pre>\n\u5b9a\u4e49\u5e38\u91cf<\/strong><\/span><\/div>\n\u5728 ES5 \u4f7f\u7528\u5b57\u7b26\u4e32\u8868\u793a\u5e38\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n
const COLOR_RED = \"red\";\r\nconst COLOR_YELLOW = \"yellow\";\r\nconst COLOR_BLUE = \"blue\";<\/pre>\n\u4f46\u662f\u7528\u5b57\u7b26\u4e32\u4e0d\u80fd\u4fdd\u8bc1\u5e38\u91cf\u662f\u72ec\u7279\u7684\uff0c\u8fd9\u6837\u4f1a\u5f15\u8d77\u4e00\u4e9b\u95ee\u9898\uff1a<\/p>\n
const COLOR_RED = \"red\";\r\nconst COLOR_YELLOW = \"yellow\";\r\nconst COLOR_BLUE = \"blue\";\r\nconst MY_BLUE = \"blue\";\r\n \r\nfunction ColorException(message) {\r\n this.message = message;\r\n this.name = \"ColorException\";\r\n}\r\n \r\nfunction getConstantName(color) {\r\n switch (color) {\r\n case COLOR_RED :\r\n return \"COLOR_RED\";\r\n case COLOR_YELLOW :\r\n return \"COLOR_YELLOW \";\r\n case COLOR_BLUE:\r\n return \"COLOR_BLUE\";\r\n case MY_BLUE:\r\n return \"MY_BLUE\"; \r\n default:\r\n throw new ColorException(\"Can't find this color\");\r\n }\r\n}\r\n \r\ntry {\r\n \r\n var color = \"green\"; \/\/ green \u5f15\u53d1\u5f02\u5e38\r\n var colorName = getConstantName(color);\r\n} catch (e) {\r\n var colorName = \"unknown\";\r\n console.log(e.message, e.name); \/\/ \u4f20\u9012\u5f02\u5e38\u5bf9\u8c61\u5230\u9519\u8bef\u5904\u7406\r\n}<\/pre>\n\u4f46\u662f\u4f7f\u7528 Symbol \u5b9a\u4e49\u5e38\u91cf\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u8fd9\u4e00\u7ec4\u5e38\u91cf\u7684\u503c\u90fd\u4e0d\u76f8\u7b49\u3002\u7528 Symbol \u6765\u4fee\u6539\u4e0a\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n
const COLOR_RED = Symbol(\"red\");\r\nconst COLOR_YELLOW = Symbol(\"yellow\");\r\nconst COLOR_BLUE = Symbol(\"blue\");\r\n \r\nfunction ColorException(message) {\r\n this.message = message;\r\n this.name = \"ColorException\";\r\n}\r\nfunction getConstantName(color) {\r\n switch (color) {\r\n case COLOR_RED :\r\n return \"COLOR_RED\";\r\n case COLOR_YELLOW :\r\n return \"COLOR_YELLOW \";\r\n case COLOR_BLUE:\r\n return \"COLOR_BLUE\";\r\n default:\r\n throw new ColorException(\"Can't find this color\");\r\n }\r\n}\r\n \r\ntry {\r\n \r\n var color = \"green\"; \/\/ green \u5f15\u53d1\u5f02\u5e38\r\n var colorName = getConstantName(color);\r\n} catch (e) {\r\n var colorName = \"unknown\";\r\n console.log(e.message, e.name); \/\/ \u4f20\u9012\u5f02\u5e38\u5bf9\u8c61\u5230\u9519\u8bef\u5904\u7406\r\n}<\/pre>\nSymbol \u7684\u503c\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u4e0d\u4f1a\u51fa\u73b0\u76f8\u540c\u503c\u5f97\u5e38\u91cf\uff0c\u5373\u53ef\u4ee5\u4fdd\u8bc1 switch \u6309\u7167\u4ee3\u7801\u9884\u60f3\u7684\u65b9\u5f0f\u6267\u884c\u3002<\/p>\n
Symbol.for()<\/strong><\/span><\/div>\nSymbol.for() \u7c7b\u4f3c\u5355\u4f8b\u6a21\u5f0f\uff0c\u9996\u5148\u4f1a\u5728\u5168\u5c40\u641c\u7d22\u88ab\u767b\u8bb0\u7684 Symbol \u4e2d\u662f\u5426\u6709\u8be5\u5b57\u7b26\u4e32\u53c2\u6570\u4f5c\u4e3a\u540d\u79f0\u7684 Symbol \u503c\uff0c\u5982\u679c\u6709\u5373\u8fd4\u56de\u8be5 Symbol \u503c\uff0c\u82e5\u6ca1\u6709\u5219\u65b0\u5efa\u5e76\u8fd4\u56de\u4e00\u4e2a\u4ee5\u8be5\u5b57\u7b26\u4e32\u53c2\u6570\u4e3a\u540d\u79f0\u7684 Symbol \u503c\uff0c\u5e76\u767b\u8bb0\u5728\u5168\u5c40\u73af\u5883\u4e2d\u4f9b\u641c\u7d22\u3002<\/p>\n
let yellow = Symbol(\"Yellow\");\r\nlet yellow1 = Symbol.for(\"Yellow\");\r\nyellow === yellow1; \/\/ false\r\n \r\nlet yellow2 = Symbol.for(\"Yellow\");\r\nyellow1 === yellow2; \/\/ true<\/pre>\nSymbol.keyFor()<\/strong><\/span><\/div>\nSymbol.keyFor() \u8fd4\u56de\u4e00\u4e2a\u5df2\u767b\u8bb0\u7684 Symbol \u7c7b\u578b\u503c\u7684 key \uff0c\u7528\u6765\u68c0\u6d4b\u8be5\u5b57\u7b26\u4e32\u53c2\u6570\u4f5c\u4e3a\u540d\u79f0\u7684 Symbol \u503c\u662f\u5426\u5df2\u88ab\u767b\u8bb0\u3002<\/p>\n
let yellow1 = Symbol.for(\"Yellow\");\r\nSymbol.keyFor(yellow1); \/\/ \"Yellow\"<\/pre>\n","protected":false},"excerpt":{"rendered":"ES6 \u6570\u636e\u7c7b\u578b\u9664\u4e86 Number \u3001 String \u3001 Boolean \u3001 Objec t\u3001 null \u548c […]<\/p>\n","protected":false},"author":321,"featured_media":197045,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-197043","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/197043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=197043"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/197043\/revisions"}],"predecessor-version":[{"id":197051,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/197043\/revisions\/197051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/197045"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=197043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=197043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=197043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}