{"id":53151,"date":"2024-12-03T13:33:27","date_gmt":"2024-12-03T05:33:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53151\/"},"modified":"2024-12-03T13:33:27","modified_gmt":"2024-12-03T05:33:27","slug":"%e4%bd%a0%e9%9c%80%e8%a6%81%e4%ba%86%e8%a7%a3%e7%9a%84-javascript-%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53151\/","title":{"rendered":"\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17326913856746c5b91c95f.jpg\" class=\"aligncenter\" title=\"\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027\u63d2\u56fe\" alt=\"\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5728\u5c1d\u8bd5\u8bbf\u95ee\u53ef\u80fd<strong>\u672a\u5b9a\u4e49\u6216 null<\/strong> \u7684\u6570\u636e\u65f6\u9632\u6b62\u9519\u8bef\uff0c\u5e76\u4e14\u6211\u4eec\u5c06\u7814\u7a76\u5728\u5fc5\u8981\u65f6\u53ef\u7528\u4e8e\u6709\u6548\u7ba1\u7406\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n<h2> \u901a\u8fc7\u53ef\u9009\u94fe\u63a5\u8fdb\u884c\u5b89\u5168\u8bbf\u95ee <\/h2>\n<p>\u5728 javascript \u4e2d\uff0c\u5f53\u5c1d\u8bd5\u8bbf\u95ee\u5d4c\u5957\u5bf9\u8c61\u4e2d\u7684\u503c\u6216\u51fd\u6570\u65f6\uff0c\u5982\u679c\u7ed3\u679c\u4e3a <strong>undefined<\/strong>\uff0c\u60a8\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u6b64\u9519\u8bef\u53ef\u80fd\u4f1a\u505c\u6b62\u4ee3\u7801\u7684\u6267\u884c\u3002\u4f46\u662f\uff0c<strong>\u5982\u679c\u4f7f\u7528\u53ef\u9009\u94fe\u63a5\u8fd0\u7b97\u7b26<\/strong>\uff0c\u5f53\u5c1d\u8bd5\u8bbf\u95ee\u5bf9\u8c61\u5185\u7684\u503c\u6216\u51fd\u6570\u65f6\uff0c\u5982\u679c\u8be5\u503c\u6216\u51fd\u6570\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u8fd4\u56de undefined \u800c\u4e0d\u662f\u629b\u51fa\u9519\u8bef\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u60a8\u7684\u4ee3\u7801\u5d29\u6e83\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>const person = {\n  name: 'john',\n  address: {\n    city: 'new york'\n  }\n};\n\nconsole.log(person.address?.city); \/\/ 'new york'\nconsole.log(person.address?.country); \/\/ undefined, no error\n\n<\/pre>\n<h2> \u7a7a\u503c\u5408\u5e76 <\/h2>\n<p>\u5982\u679c\u53d8\u91cf\u7684\u503c\u4e3a<strong>null \u6216\u672a\u5b9a\u4e49<\/strong>\uff0c\u5b83\u53ef\u80fd\u4f1a\u7834\u574f\u60a8\u7684\u4ee3\u7801\u3002\u8981\u5728\u53d8\u91cf\u4e3a null \u6216\u672a\u5b9a\u4e49\u65f6\u5206\u914d\u9ed8\u8ba4\u503c\uff0c** \u60a8\u53ef\u4ee5\u4f7f\u7528 nullish \u5408\u5e76\u8fd0\u7b97\u7b26 (??)\u3002**<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>function getconfig(config) {\n    return config ?? { timeout: 1000, retries: 3 };\n}\n\nlet userconfig = null;\nlet finalconfig = getconfig(userconfig); \/\/ { timeout: 1000, retries: 3 } \nconsole.log(finalconfig);\n\n<\/pre>\n<h2> \u4f7f\u7528 set \u548c weakset \u7ba1\u7406\u91cd\u590d\u9879 <\/h2>\n<p><strong>\u4f7f\u7528 set \u5220\u9664\u91cd\u590d\u9879\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u6570\u7ec4\u5305\u542b<strong>\u91cd\u590d\u503c<\/strong>\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 set \u5220\u9664\u8fd9\u4e9b\u91cd\u590d\u9879\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u6b64\u7ed3\u6784\uff1a<\/p>\n<pre>const numbers = [1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9];\nconst uniquenumbers = [...new set(numbers)];\n\nconsole.log(uniquenumbers); \/\/ [1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n<\/pre>\n<p><strong>\u4f7f\u7528 wea\u200b\u200bkset \u9632\u6b62\u91cd\u590d\uff1a<\/strong><\/p>\n<p>\u7531\u4e8e<strong>weakset \u4fdd\u5b58\u5bf9\u5bf9\u8c61<\/strong> \u7684\u5f15\u7528\uff0c\u56e0\u6b64\u4e00\u4e2a\u5bf9\u8c61\u53ea\u80fd\u6dfb\u52a0\u5230weakset \u4e00\u6b21\u3002\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>\/\/ Creating a WeakSet\nconst weakset = new WeakSet();\n\n\/\/ Defining objects\nconst obj1 = { name: 'Alice' };\nconst obj2 = { name: 'Bob' };\n\n\/\/ Adding objects to the WeakSet\nweakset.add(obj1);\nweakset.add(obj2);\n\nconsole.log(weakset.has(obj1)); \/\/ true\nconsole.log(weakset.has(obj2)); \/\/ true\n\n\/\/ Attempting to add the same object again\nweakset.add(obj1); \/\/ obj1 is already present, won't be added again\n\nconsole.log(weakset.has(obj1)); \/\/ true\nconsole.log(weakset.has(obj2)); \/\/ true\n\n\/\/ Removing an object from the WeakSet\nweakset.delete(obj1);\nconsole.log(weakset.has(obj1)); \/\/ false\n\n\/\/ Adding the object again\nweakset.add(obj1);\nconsole.log(weakset.has(obj1)); \/\/ true\n\n<\/pre>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u4e00\u4e9b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u8fd9\u4e9b\u6982\u5ff5\u53ef\u4ee5\u5e2e\u52a9\u9632\u6b62\u5728\u8bbf\u95ee\u53ef\u80fd\u672a\u5b9a\u4e49\u6216\u4e3a null \u7684\u503c\u65f6\u53d1\u751f\u9519\u8bef\uff0c\u4ee5\u53ca\u5728\u5fc5\u8981\u65f6\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   ES6\u4e2dstatic\u548csuper\u5173\u952e\u5b57\u5728\u7ee7\u627f\u65f6\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u8f93\u51fa\u7ed3\u679c\u89e3\u6790\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300a\u4f60\u9700\u8981\u4e86\u89e3\u7684 JavaScript \u7279\u6027\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5728\u5c1d\u8bd5\u8bbf\u95ee\u53ef\u80fd\u672a\u5b9a\u4e49\u6216 null \u7684\u6570\u636e\u65f6\u9632\u6b62\u9519\u8bef\uff0c\u5e76\u4e14\u6211\u4eec\u5c06\u7814\u7a76\u5728\u5fc5\u8981\u65f6\u53ef\u7528\u4e8e\u6709\u6548\u7ba1\u7406\u6570\u636e\u7684\u65b9\u6cd5\u3002 \u901a\u8fc7\u53ef\u9009\u94fe\u63a5\u8fdb\u884c\u5b89\u5168\u8bbf\u95ee \u5728 javascript \u4e2d\uff0c\u5f53\u5c1d\u8bd5\u8bbf\u95ee\u5d4c\u5957\u5bf9\u8c61\u4e2d\u7684\u503c\u6216\u51fd\u6570\u65f6\uff0c\u5982\u679c\u7ed3\u679c\u4e3a undefined\uff0c\u60a8\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u6b64\u9519\u8bef\u53ef\u80fd\u4f1a\u505c\u6b62\u4ee3\u7801\u7684\u6267\u884c\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u53ef\u9009\u94fe\u63a5\u8fd0\u7b97\u7b26\uff0c\u5f53\u5c1d\u8bd5\u8bbf\u95ee\u5bf9\u8c61\u5185\u7684\u503c\u6216\u51fd\u6570\u65f6\uff0c\u5982\u679c\u8be5\u503c\u6216\u51fd\u6570\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u8fd4\u56de undefined \u800c\u4e0d\u662f\u629b\u51fa\u9519\u8bef\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u60a8\u7684\u4ee3\u7801\u5d29\u6e83\u3002 \u793a\u4f8b\uff1a const person = { name: &#8216;john&#8217;, address: { city: &#8216;new york&#8217; } }; console.log(person.address?.city); \/\/ &#8216;new york&#8217; console.log(person.address?.country); \/\/ undefined, no error \u7a7a\u503c\u5408\u5e76 \u5982\u679c\u53d8\u91cf\u7684\u503c\u4e3anull \u6216\u672a\u5b9a\u4e49\uff0c\u5b83\u53ef\u80fd\u4f1a\u7834\u574f\u60a8\u7684\u4ee3\u7801\u3002\u8981\u5728\u53d8\u91cf\u4e3a null \u6216\u672a\u5b9a\u4e49\u65f6\u5206\u914d\u9ed8\u8ba4\u503c\uff0c** \u60a8\u53ef\u4ee5\u4f7f\u7528 nullish \u5408\u5e76\u8fd0\u7b97\u7b26 (??)\u3002** \u793a\u4f8b\uff1a function getconfig(config) { return config [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53151","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53151"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53151\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}