{"id":2061,"date":"2025-02-26T23:23:03","date_gmt":"2025-02-26T15:23:03","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2061"},"modified":"2025-02-26T23:23:03","modified_gmt":"2025-02-26T15:23:03","slug":"javascript-%e4%b8%a5%e6%a0%bc%e6%a8%a1%e5%bc%8f-use-strict","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/26\/javascript-%e4%b8%a5%e6%a0%bc%e6%a8%a1%e5%bc%8f-use-strict\/","title":{"rendered":"JavaScript \u4e25\u683c\u6a21\u5f0f (&#8220;use strict&#8221;)"},"content":{"rendered":"\n<p>JavaScript \u7684\u4e25\u683c\u6a21\u5f0f\uff08<code>\"use strict\"<\/code>\uff09\u662f\u4e00\u79cd\u589e\u5f3a\u7684\u9519\u8bef\u68c0\u67e5\u6a21\u5f0f\uff0c\u5b83\u4f7f JavaScript \u7684\u6267\u884c\u66f4\u52a0\u4e25\u683c\uff0c\u907f\u514d\u4e86\u8bb8\u591a\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002\u4e25\u683c\u6a21\u5f0f\u901a\u8fc7\u5bf9\u4e00\u4e9b\u5bbd\u677e\u7684\u884c\u4e3a\u8fdb\u884c\u9650\u5236\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u66f4\u5b89\u5168\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u542f\u7528\u4e25\u683c\u6a21\u5f0f<\/strong><\/h3>\n\n\n\n<p>\u4e25\u683c\u6a21\u5f0f\u53ef\u4ee5\u901a\u8fc7\u5728 JavaScript \u4ee3\u7801\u7684\u5f00\u5934\u6dfb\u52a0 <code>\"use strict\"<\/code> \u6765\u542f\u7528\u3002\u4e25\u683c\u6a21\u5f0f\u53ef\u4ee5\u5728\u6574\u4e2a\u6587\u4ef6\u4e2d\u542f\u7528\uff0c\u6216\u8005\u53ea\u5728\u51fd\u6570\u5185\u90e8\u542f\u7528\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5168\u5c40\u4e25\u683c\u6a21\u5f0f<\/strong>\uff1a\u5728\u811a\u672c\u7684\u9876\u90e8\u6dfb\u52a0 <code>\"use strict\"<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u51fd\u6570\u7ea7\u4e25\u683c\u6a21\u5f0f<\/strong>\uff1a\u5728\u51fd\u6570\u7684\u9876\u90e8\u6dfb\u52a0 <code>\"use strict\"<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5168\u5c40\u542f\u7528\u4e25\u683c\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\n\nvar x = 10;  \/\/ \u6b63\u5e38<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u51fd\u6570\u7ea7\u542f\u7528\u4e25\u683c\u6a21\u5f0f<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function strictFunction() {\n  \"use strict\";\n  var y = 20;  \/\/ \u5728\u51fd\u6570\u5185\u542f\u7528\u4e25\u683c\u6a21\u5f0f\n}\n\nstrictFunction();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4e25\u683c\u6a21\u5f0f\u4e0b\u7684\u9650\u5236\u4e0e\u53d8\u5316<\/strong><\/h3>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0cJavaScript \u4f1a\u9650\u5236\u4e00\u4e9b\u64cd\u4f5c\uff0c\u9632\u6b62\u4e00\u4e9b\u6f5c\u5728\u7684\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e25\u683c\u6a21\u5f0f\u4e0b\u7684\u4e3b\u8981\u53d8\u5316\u548c\u9650\u5236\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. \u7981\u6b62\u4f7f\u7528\u672a\u58f0\u660e\u7684\u53d8\u91cf<\/strong><\/h4>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u4f7f\u7528\u672a\u7ecf\u58f0\u660e\u7684\u53d8\u91cf\u4f1a\u5bfc\u81f4 <code>ReferenceError<\/code> \u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nx = 5;  \/\/ ReferenceError: x is not defined<\/code><\/pre>\n\n\n\n<p>\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u8fd9\u79cd\u60c5\u51b5\u4f1a\u9690\u5f0f\u5730\u5c06 <code>x<\/code> \u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u8fdb\u884c\u521b\u5efa\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. \u7981\u6b62\u5220\u9664\u53d8\u91cf\u3001\u51fd\u6570\u548c\u51fd\u6570\u53c2\u6570<\/strong><\/h4>\n\n\n\n<p>\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u4e0d\u80fd\u5220\u9664\u53d8\u91cf\u3001\u51fd\u6570\u548c\u51fd\u6570\u53c2\u6570\u3002\u8fd9\u4f1a\u629b\u51fa <code>SyntaxError<\/code> \u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nvar x = 10;\ndelete x;  \/\/ SyntaxError: Delete of an unqualified identifier in strict mode.<\/code><\/pre>\n\n\n\n<p>\u4f60\u53ea\u80fd\u5220\u9664\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u80fd\u5220\u9664\u53d8\u91cf\u6216\u51fd\u6570\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. \u7981\u6b62\u4f7f\u7528 <code>eval<\/code> \u4f5c\u4e3a\u53d8\u91cf\u540d<\/strong><\/h4>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c<code>eval<\/code> \u4e0d\u80fd\u4f5c\u4e3a\u6807\u8bc6\u7b26\uff08\u4f8b\u5982\uff0c\u53d8\u91cf\u540d\u6216\u51fd\u6570\u540d\uff09\uff0c\u5e76\u4e14\u5728\u4e25\u683c\u6a21\u5f0f\u4e2d\u4f7f\u7528 <code>eval<\/code> \u4e5f\u4f1a\u6709\u4e0d\u540c\u7684\u4f5c\u7528\u57df\u884c\u4e3a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nvar eval = 10;  \/\/ SyntaxError: Unexpected eval or arguments in strict mode<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4. \u7981\u6b62\u91cd\u590d\u53c2\u6570\u540d<\/strong><\/h4>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u51fd\u6570\u7684\u53c2\u6570\u4e0d\u80fd\u6709\u91cd\u590d\u7684\u540d\u5b57\uff0c\u5426\u5219\u4f1a\u629b\u51fa <code>SyntaxError<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nfunction foo(a, a) {  \/\/ SyntaxError: Duplicate parameter name not allowed in this context\n  return a + a;\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0cJavaScript \u4f1a\u63a5\u53d7\u8fd9\u79cd\u91cd\u590d\u7684\u53c2\u6570\u540d\uff0c\u4f46\u5b83\u4f1a\u4f7f\u7528\u6700\u540e\u4e00\u4e2a\u5b9a\u4e49\u7684\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>5. <code>this<\/code> \u7684\u884c\u4e3a\u53d8\u5316<\/strong><\/h4>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c<code>this<\/code> \u7684\u503c\u4e0d\u4f1a\u88ab\u81ea\u52a8\u7ed1\u5b9a\u5230\u5168\u5c40\u5bf9\u8c61\u3002\u5728\u975e\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u5982\u679c\u5728\u51fd\u6570\u4e2d\u6ca1\u6709\u660e\u786e\u7684 <code>this<\/code> \u503c\uff0c\u5b83\u4f1a\u81ea\u52a8\u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff08\u5728\u6d4f\u89c8\u5668\u4e2d\u662f <code>window<\/code>\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nfunction myFunction() {\n  console.log(this);  \/\/ undefined\uff0c\u800c\u4e0d\u662f window\n}\n\nmyFunction();<\/code><\/pre>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c<code>this<\/code> \u7684\u9ed8\u8ba4\u503c\u662f <code>undefined<\/code>\uff0c\u800c\u4e0d\u662f\u5168\u5c40\u5bf9\u8c61\uff08\u5982 <code>window<\/code>\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>6. <code>with<\/code> \u8bed\u53e5\u7684\u7981\u6b62\u4f7f\u7528<\/strong><\/h4>\n\n\n\n<p><code>with<\/code> \u8bed\u53e5\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u88ab\u7981\u6b62\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bfc\u81f4\u4f5c\u7528\u57df\u89e3\u6790\u7684\u4e0d\u660e\u786e\u6027\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nwith (Math) {  \/\/ SyntaxError: Strict mode code may not include a with statement\n  var result = cos(2);\n}<\/code><\/pre>\n\n\n\n<p><code>with<\/code> \u8bed\u53e5\u5141\u8bb8\u4f60\u5728\u7279\u5b9a\u7684\u4f5c\u7528\u57df\u5185\u8bbf\u95ee\u591a\u4e2a\u5c5e\u6027\uff0c\u4f46\u5b83\u4f1a\u589e\u52a0\u4ee3\u7801\u590d\u6742\u5ea6\uff0c\u5bfc\u81f4\u6f5c\u5728\u7684\u4f5c\u7528\u57df\u95ee\u9898\uff0c\u56e0\u6b64\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u88ab\u7981\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>7. \u7981\u6b62 <code>octal<\/code>\uff08\u516b\u8fdb\u5236\u5b57\u9762\u91cf\uff09\u8bed\u6cd5<\/strong><\/h4>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u4e0d\u80fd\u4f7f\u7528\u516b\u8fdb\u5236\u5b57\u9762\u91cf\uff08\u5982 <code>010<\/code>\uff09\uff0c\u5426\u5219\u4f1a\u629b\u51fa <code>SyntaxError<\/code> \u9519\u8bef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nvar x = 010;  \/\/ SyntaxError: Octal literals are not allowed in strict mode.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>8. <code>eval<\/code> \u548c <code>arguments<\/code> \u7684\u7279\u6b8a\u5904\u7406<\/strong><\/h4>\n\n\n\n<p>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c<code>eval<\/code> \u548c <code>arguments<\/code> \u4e0d\u80fd\u4f5c\u4e3a\u53d8\u91cf\u540d\u3001\u51fd\u6570\u540d\u6216\u5c5e\u6027\u540d\u3002\u6b64\u5916\uff0c\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c<code>eval<\/code> \u4e5f\u4e0d\u80fd\u521b\u5efa\u6216\u4fee\u6539\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"use strict\";\nvar eval = 10;  \/\/ SyntaxError: Unexpected eval or arguments in strict mode<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u4e25\u683c\u6a21\u5f0f\u7684\u597d\u5904<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u5b89\u5168\u6027<\/strong>\uff1a\u4e25\u683c\u6a21\u5f0f\u6d88\u9664\u4e86 JavaScript \u4e2d\u7684\u4e00\u4e9b\u5e38\u89c1\u9519\u8bef\uff0c\u5982\u9690\u5f0f\u5168\u5c40\u53d8\u91cf\u7684\u521b\u5efa\u3001\u91cd\u590d\u7684\u53c2\u6570\u540d\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u589e\u5f3a\u4ee3\u7801\u7684\u6027\u80fd<\/strong>\uff1a\u4e00\u4e9b\u4e25\u683c\u6a21\u5f0f\u4e0b\u7684\u9650\u5236\u4f7f JavaScript \u5f15\u64ce\u53ef\u4ee5\u4f18\u5316\u4ee3\u7801\u6267\u884c\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/li>\n\n\n\n<li><strong>\u51cf\u5c11\u9690\u5f0f\u9519\u8bef<\/strong>\uff1a\u4e25\u683c\u6a21\u5f0f\u80fd\u8ba9\u5f00\u53d1\u8005\u66f4\u65e9\u53d1\u73b0\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u5982\u4e0d\u5f53\u4f7f\u7528\u53d8\u91cf\u3001\u51fd\u6570\u53c2\u6570\u3001\u5220\u9664\u64cd\u4f5c\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u517c\u5bb9\u6027<\/strong><\/h3>\n\n\n\n<p>\u4e25\u683c\u6a21\u5f0f\u4ece ECMAScript 5 \u5f00\u59cb\u5f15\u5165\uff0c\u5e76\u5728\u540e\u7eed\u7248\u672c\u4e2d\u5f97\u5230\u5e7f\u6cdb\u652f\u6301\u3002\u5927\u591a\u6570\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u652f\u6301\u4e25\u683c\u6a21\u5f0f\uff0c\u4f46\u5728\u8f83\u8001\u7684\u6d4f\u89c8\u5668\u4e2d\uff0c\u53ef\u80fd\u4e0d\u5b8c\u5168\u652f\u6301\u8be5\u6a21\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e25\u683c\u6a21\u5f0f\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u542f\u7528 <code>\"use strict\"<\/code> \u6765\u5e2e\u52a9\u4f60\u7f16\u5199\u66f4\u4e25\u683c\u3001\u66f4\u5b89\u5168\u7684 JavaScript \u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u5b83\u901a\u8fc7\u9650\u5236\u4e00\u4e9b\u5bbd\u677e\u7684\u884c\u4e3a\uff0c\u907f\u514d\u4e86\u6f5c\u5728\u7684\u9519\u8bef\u548c\u4e0d\u89c4\u8303\u7684\u7528\u6cd5\u3002<\/li>\n\n\n\n<li>\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7981\u6b62\u4f7f\u7528\u672a\u58f0\u660e\u7684\u53d8\u91cf\u3001\u5220\u9664\u64cd\u4f5c\u3001\u91cd\u590d\u7684\u51fd\u6570\u53c2\u6570\u3001<code>with<\/code> \u8bed\u53e5\u7b49\u3002<\/li>\n\n\n\n<li>\u542f\u7528\u4e25\u683c\u6a21\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u5e76\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u7f16\u5199\u73b0\u4ee3\u3001\u5e72\u51c0\u4e14\u66f4\u5b89\u5168\u7684 JavaScript \u4ee3\u7801\uff0c\u63a8\u8350\u59cb\u7ec8\u4f7f\u7528\u4e25\u683c\u6a21\u5f0f\u3002\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u7684\u4e25\u683c\u6a21\u5f0f\uff08&#8221;use strict&#8221;\uff09\u662f\u4e00\u79cd\u589e\u5f3a\u7684\u9519\u8bef\u68c0\u67e5\u6a21\u5f0f\uff0c\u5b83\u4f7f JavaScrip [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2061","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2061"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2061\/revisions"}],"predecessor-version":[{"id":2062,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2061\/revisions\/2062"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}