{"id":237410,"date":"2022-03-22T08:26:00","date_gmt":"2022-03-22T00:26:00","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=237410"},"modified":"2022-03-11T11:26:37","modified_gmt":"2022-03-11T03:26:37","slug":"data-types-added-to-javascript","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/data-types-added-to-javascript.html","title":{"rendered":"\u62a2\u5148\u770b\uff1aJavaScript\u7684\u65b0\u589e\u4e24\u4e2a\u539f\u59cb\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJavaScript\u5373\u5c06\u63a8\u51fa\u4e24\u4e2a\u65b0\u7684\u6570\u636e\u7c7b\u578b\uff1aRecord \u548c Tuple \uff0c\u8fd9\u4fe9\u662f\u5565\u5462\uff1f\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u53ea\u8bfb\u7684 Object \u548c Array\uff0c\u5176\u5b9e\u5728\u5176\u5b83\u8bed\u8a00\u4e2d\u5df2\u7ecf\u6709\u7c7b\u4f3c\u7684\u6570\u636e\u7c7b\u578b\u4e86\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nJavaScript\u5373\u5c06\u63a8\u51fa\u4e24\u4e2a\u65b0\u7684\u6570\u636e\u7c7b\u578b\uff1aRecord \u548c Tuple \uff0c\u8fd9\u4fe9\u662f\u5565\u5462\uff1f\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u53ea\u8bfb\u7684 Object \u548c Array\uff0c\u5176\u5b9e\u5728\u5176\u5b83\u8bed\u8a00\u4e2d\u5df2\u7ecf\u6709\u7c7b\u4f3c\u7684\u6570\u636e\u7c7b\u578b\u4e86\u3002<\/p>\n

\u57fa\u7840\u5199\u6cd5<\/strong><\/div>\n
\r\n\/\/ Records\r\nconst myRecord = #{\r\n name: '01',\r\n  age: 23\r\n}\r\n\/\/ Tuple\r\nconst myTuple = #['1', '2', '3']\r\n<\/pre>\n

\u5176\u5b9e\u5c31\u662f\u5728\u539f\u5148\u7684\u5bf9\u8c61\u548c\u6570\u7ec4\u524d\u9762\u52a0\u4e86\u4e2a #<\/p>\n

\u53ef\u8bfb\u7279\u6027<\/strong><\/div>\n

Record\u548cTuple\u7684\u8bed\u6cd5\u8ddf\u5bf9\u8c61\u548c\u6570\u7ec4\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\uff1f<\/p>\n

\r\nconst myRecord = #{\r\n name: '01'\r\n}\r\nconst myTuple = #['1', '2']\r\nmyRecord['age'] = 23  \/\/ error\r\nmyTuple.push('3')  \/\/ error\r\n<\/pre>\n

\u4e3a\u5565\u62a5\u9519\u4e86\u554a\uff1f\u5f00\u5934\u6709\u63d0\u5230\u54e6~\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u7c7b\u578b\u662f \u53ea\u8bfb\u7684 Object \u548c Array<\/p>\n

\u975e\u552f\u4e00\u6027<\/strong><\/div>\n

\u5728\u5e73\u65f6\u7684\u5f00\u53d1\u4e2d\uff0c\u6570\u7ec4\u4e0e\u6570\u7ec4\u3001\u5bf9\u8c61\u4e0e\u5bf9\u8c61 \u90fd\u4e0d\u9002\u5408\u76f4\u63a5\u7528 === \u8fdb\u884c\u6bd4\u8f83\u5224\u65ad\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u751f\u6210\u7684\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740\u90fd\u4e0d\u4e00\u6837<\/p>\n

\r\nconst obj1 = { name: '01' }\r\nconst obj2 = { name: '01' }\r\nconst objIsSame = obj1 === obj2   \/\/ false\r\nconst arr1 = [1]\r\nconst arr2 = [1]\r\nconst arrIsSame = arr1 === arr2   \/\/ false\r\n<\/pre>\n

\u8981\u60f3\u771f\u6b63\u6bd4\u8f83\u4e24\u4e2a\u6570\u7ec4\u6216\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\uff08\u5373\u6211\u4eec\u60f3\u8981\u7684\u5185\u5bb9\u90fd\u4e00\u6837\uff09\uff0c\u9700\u8981\u904d\u5386\u9012\u5f52\u53bb\u4e00\u4e00\u5bf9\u6bd4\uff0c\u800c\u73b0\u5728\u5462\uff1fRecord\u548cTuple\u80fd\u5426\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u5462\uff1f<\/p>\n

\r\nconst record1 = #{ name: '01' }\r\nconst record2 = #{ name: '01' }\r\nconst recordIsSame = record1 === record2   \/\/ true\r\nconst tuple1 = #[1]\r\nconst tuple2 = #[1]\r\nconst tupleIsSame = tuple1 === tuple2   \/\/ true\r\n<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u53ea\u8981\u5185\u90e8\u5185\u5bb9\u4e00\u81f4\uff0c\u5373\u4f7f\u662f\u4e24\u4e2a\u5206\u522b\u751f\u6210\u7684Record\u6216Tuple\u6bd4\u8f83\u4e00\u4e0b\uff0c\u4e5f\u662f\u76f8\u7b49\u7684<\/p>\n

\u666e\u901a\u5bf9\u8c61\u548c\u6570\u7ec4\u7684\u8f6c\u6362<\/strong><\/div>\n

\u6211\u53ef\u4ee5\u7528\u5bf9\u8c61 Record \u548c Tuple \u5c06\u666e\u901a\u7684\u5bf9\u8c61\u548c\u6570\u7ec4\u8f6c\u6362<\/p>\n

\r\nconst myRecord = Record({ name: '01', age: 23 });   \/\/ #{ name: '01', age: 23 }\r\nconst myTuple = Tuple([1, 2, 3, 4, 5]);   \/\/ #[1, 2, 3, 4, 5]\r\n<\/pre>\n
\u652f\u6301\u6269\u5c55\u8fd0\u7b97\u7b26<\/strong><\/div>\n

\u6211\u4eec\u4e5f\u53ef\u4ee5\u5bf9Record\u548cTuple\u4f7f\u7528\u6269\u5c55\u8fd0\u7b97\u7b26<\/p>\n

const myTuple = #[1, 2, 3];
\nconst myRecord = #{ name: '01', age: 23 };
\nconst newRecord = #{ ...myRecord, money: 0 } \/\/ #{ name: '01', age: 23, money: 0 }
\nconst newTuple = #[ ...myTuple, 4, 5]; \/\/ #[1, 2, 3, 4, 5]<\/p>\n

JSON\u65b9\u6cd5\u6269\u5c55<\/strong><\/div>\n

\u73b0\u5728\u4e0d\u662f\u6709 JSON.parse \u548c JSON.stringfy \u4e24\u4e2a\u65b9\u6cd5\u561b\uff0c\u636e\u8bf4\u8349\u6848\u4e2d\u8fd8\u63d0\u5230\u4e00\u4e2a\u4e0d\u9519\u7684\u60f3\u6cd5\uff0c\u90a3\u5c31\u662f\u7ed9 JSON \u5bf9\u8c61\u65b0\u589e\u4e00\u4e2a parseImmutable \u65b9\u6cd5\uff0c\u529f\u80fd\u5e94\u8be5\u5c31\u662f\u76f4\u63a5\u5c06\u4e00\u4e2a Record\u5b57\u7b26\u4e32\u6216Tuple\u5b57\u7b26\u4e32 \u89e3\u6790\u6210\u5bf9\u5e94\u7684Record\u548cTuple\u5bf9\u8c61<\/p>\n

\u63d0\u524d\u4f53\u9a8c<\/strong><\/div>\n

\u5982\u679c\u4f60\u60f3\u73b0\u5728\u4f53\u9a8c\u8be5\u529f\u80fd\uff0c\u53ef\u4ee5\u88c5\u4e00\u4e0bbabel\u7684\u63d2\u4ef6<\/p>\n

\r\n# babel\u57fa\u672c\u7684\u5e93\r\nyarn add @babel\/cli @babel\/core @babel\/preset-env -D\r\n# Record\u548cTuple Babel polyfill\r\nyarn add @babel\/plugin-proposal-record-and-tuple @bloomberg\/record-tuple-polyfill -D\r\n<\/pre>\n

\u5728\u76ee\u5f55\u4e0b\u521b\u5efa .babelrc\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

\r\n{\r\n    \"presets\": [\"@babel\/preset-env\"],\r\n    \"plugins\": [\r\n        [\r\n          \"@babel\/plugin-proposal-record-and-tuple\",\r\n          {\r\n            \"importPolyfill\": true,\r\n            \"syntaxType\": \"hash\"\r\n          }\r\n        ]\r\n      ]\r\n}\r\n<\/pre>\n

\u518d\u521b\u5efa\u4e00\u4e2a index.js\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

\r\nconst tuple1 = #[1,2,3]\r\nconst tuple2 = #[1,2,3]\r\nconst record1 = #{ name: '01' }\r\nconst record2 = #{ name: '02' }\r\nconsole.log(tuple1 === tuple2, record1 === record2)\r\n<\/pre>\n

\u6267\u884c\u4e00\u4e0bbabel\u7684\u547d\u4ee4\u7f16\u8bd1\u4e00\u4e0b<\/p>\n

\r\n.\/node_modules\/.bin\/babel index.js --out-file compiled.js\r\n\r\n<\/pre>\n

\u8f93\u51fa\u5f97\u5230\u7684 compiled.js \u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n

\r\n\"use strict\";\r\nvar _recordTuplePolyfill = require(\"@bloomberg\/record-tuple-polyfill\");\r\nvar tuple1 = (0, _recordTuplePolyfill.Tuple)(1, 2, 3);\r\nvar tuple2 = (0, _recordTuplePolyfill.Tuple)(1, 2, 3);\r\nvar record1 = (0, _recordTuplePolyfill.Record)({\r\n  name: '01'\r\n});\r\nvar record2 = (0, _recordTuplePolyfill.Record)({\r\n  name: '02'\r\n});\r\nconsole.log(tuple1 === tuple2, record1 === record2);\r\n<\/pre>\n

\u6700\u540e\u6267\u884c compiled.js \u5373\u53ef\u83b7\u5f97\u7ed3\u679c<\/p>\n

\r\nnode compiled.js\r\n# Result: true false\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

JavaScript\u5373\u5c06\u63a8\u51fa\u4e24\u4e2a\u65b0\u7684\u6570\u636e\u7c7b\u578b\uff1aRecord \u548c Tuple \uff0c\u8fd9\u4fe9\u662f\u5565\u5462\uff1f\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u53ea\u8bfb\u7684 […]<\/p>\n","protected":false},"author":1898,"featured_media":237413,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[827],"class_list":["post-237410","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-827"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237410","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\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=237410"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237410\/revisions"}],"predecessor-version":[{"id":237416,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237410\/revisions\/237416"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/237413"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=237410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=237410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=237410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}