小白教程
所有教程
关于
Search
172.69.58.116
172.69.58.116
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“JavaScript 严格模式”的源代码
本页内容
上一节:
JavaScript_变量提升
下一节:
JavaScript_使用误区
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:JavaScript 严格模式}}[[Category:Javascript 教程|29]] = JavaScript 严格模式(use strict) = JavaScript 严格模式(strict mode)即在严格的条件下运行。 == 使用 "use strict" 指令 == "use strict" 指令在 JavaScript 1.8.5 (ECMAScript5) 中新增。 它不是一条语句,但是是一个字面量表达式,在 JavaScript 旧版本中会被忽略。 "use strict" 的目的是指定代码在严格条件下执行。 严格模式下你不能使用未声明的变量。 <blockquote>支持严格模式的浏览器:Internet Explorer 10 +、Firefox 4+、 Chrome 13+、 Safari 5.1+、 Opera 12+。</blockquote> == 严格模式声明 == 严格模式通过在脚本或函数的头部添加 use strict; 表达式来声明。 实例中我们可以在浏览器按下 '''F12 (或点击"工具>更多工具>开发者工具")''' 开启调试模式,查看报错信息。 也可以通过右击鼠标,选择 '''"检查"''' 来查看,Gif 图演示如下: == <sample title="" desc="" lang="html" hererun="1"> "use strict"; x = 3.14; // 报错 (x 未定义) </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许使用未定义的变量。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; x = 3.14; // 报错 (x 未定义) </script> </body> </html> </run> == <sample title="" desc="" lang="html" hererun="1"> "use strict"; myFunction(); function myFunction() { y = 3.14; // 报错 (y 未定义) } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h2>全局 "use strict" 声明.</h2> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; myFunction(); function myFunction() { y = 3.14; // 报错 (y 未定义) } </script> </body> </html> </run> 在函数内部声明是局部作用域 (只在函数内使用严格模式): == <sample title="" desc="" lang="html" hererun="1"> x = 3.14; // 不报错 myFunction(); function myFunction() { "use strict"; y = 3.14; // 报错 (y 未定义) } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p>在函数内使用 "use strict" 只在函数内报错。 </p> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> x = 3.14; // 不报错 myFunction(); function myFunction() { "use strict"; y = 3.14; // 报错 (y 未定义) } </script> </body> </html> </run> 为什么使用严格模式: * 消除代码运行的一些不安全之处,保证代码运行的安全; * 提高编译器效率,增加运行速度; * 为未来新版本的Javascript做好铺垫。 "严格模式"体现了Javascript更合理、更安全、更严谨的发展方向,包括IE 10在内的主流浏览器,都已经支持它,许多大项目已经开始全面拥抱它。 另一方面,同样的代码,在"严格模式"中,可能会有不一样的运行结果;一些在"正常模式"下可以运行的语句,在"严格模式"下将不能运行。掌握这些内容,有助于更细致深入地理解Javascript,让你变成一个更好的程序员。 == 严格模式的限制 == 不允许使用未声明的变量: <sample title="" desc="" lang="html" hererun="1"> "use strict"; x = 3.14; // 报错 (x 未定义) </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许使用未定义的变量。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; x = 3.14; // 报错 (x 未定义) </script> </body> </html> </run> <blockquote>对象也是一个变量。</blockquote> <sample title="" desc="" lang="html" hererun="1"> "use strict"; x = {p1:10, p2:20}; // 报错 (x 未定义) </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许使用未定义的对象。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; x = {p1:10, p2:20}; // 报错 (x 未定义) </script> </body> </html> </run> 不允许删除变量或对象。 <sample title="" desc="" lang="html" hererun="1"> "use strict"; var x = 3.14; delete x; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许删除变量或对象。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var x = 3.14; delete x; </script> </body> </html> </run> 不允许删除函数。 <sample title="" desc="" lang="html" hererun="1"> "use strict"; function x(p1, p2) {}; delete x; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许删除函数。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; function x(p1, p2) {}; delete x; </script> </body> </html> </run> 不允许变量重名: <sample title="" desc="" lang="html" hererun="1"> "use strict"; function x(p1, p1) {}; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许变量重名。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; function x(p1, p1) {}; // 报错 </script> </body> </html> </run> 不允许使用八进制: <sample title="" desc="" lang="html" hererun="1"> "use strict"; var x = 010; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许使用八进制。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var x = 010; // 报错 </script> </body> </html> </run> 不允许使用转义字符: <sample title="" desc="" lang="html" hererun="1"> "use strict"; var x = \010; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许使用转义字符。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var x = \010; // 报错 </script> </body> </html> </run> 不允许对只读属性赋值: <sample title="" desc="" lang="html" hererun="1"> "use strict"; var obj = {}; Object.defineProperty(obj, "x", {value:0, writable:false}); obj.x = 3.14; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许对只读属性赋值。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var obj = {}; Object.defineProperty(obj, "x", {value:0, writable:false}); obj.x = 3.14; // 报错 </script> </body> </html> </run> 不允许对一个使用getter方法读取的属性进行赋值 <sample title="" desc="" lang="html" hererun="1"> "use strict"; var obj = {get x() {return 0} }; obj.x = 3.14; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许对一个使用getter方法读取的属性进行赋值。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var obj = {get x() {return 0} }; obj.x = 3.14; // 报错 </script> </body> </html> </run> 不允许删除一个不允许删除的属性: <sample title="" desc="" lang="html" hererun="1"> "use strict"; delete Object.prototype; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许删除一个不允许删除的属性值。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; delete Object.prototype; // 报错 </script> </body> </html> </run> 变量名不能使用 "eval" 字符串: <sample title="" desc="" lang="html" hererun="1"> "use strict"; var eval = 3.14; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>变量名不能使用 "eval" 字符串。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var eval = 3.14; // 报错 </script> </body> </html> </run> 变量名不能使用 "arguments" 字符串: <sample title="" desc="" lang="html" hererun="1"> "use strict"; var arguments = 3.14; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>变量名不能使用 "arguments" 字符串。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var arguments = 3.14; // 报错 </script> </body> </html> </run> 不允许使用以下这种语句: <sample title="" desc="" lang="html" hererun="1"> "use strict"; with (Math){x = cos(2)}; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>不允许使用以下这种语句。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; with (Math){x = cos(2)}; // 报错 </script> </body> </html> </run> 由于一些安全原因,在作用域 eval() 创建的变量不能被调用: <sample title="" desc="" lang="html" hererun="1"> "use strict"; eval ("var x = 2"); alert (x); // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>由于一些安全原因,在作用域 eval() 创建的变量不能被调用。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; eval ("var x = 2"); alert (x); // 报错 </script> </body> </html> </run> 禁止this关键字指向全局对象。 <pre> function f(){ return !this; } // 返回false,因为"this"指向全局对象,"!this"就是false function f(){ "use strict"; return !this; } // 返回true,因为严格模式下,this的值为undefined,所以"!this"为true。 </pre> 因此,使用构造函数时,如果忘了加new,this不再指向全局对象,而是报错。 <pre> function f(){ "use strict"; this.a = 1; }; f();// 报错,this未定义 </pre> == 保留关键字 == 为了向将来Javascript的新版本过渡,严格模式新增了一些保留关键字: * implements * interface * let * package * private * protected * public * static * yield <sample title="" desc="" lang="html" hererun="1"> "use strict"; var public = 1500; // 报错 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <h1>使用 "use strict":</h1> <h3>严格模式不允许使用保留关键字。</h3> <p>浏览器按下 F12 开启调试模式,查看报错信息。</p> <script> "use strict"; var public = 1500; // 报错 </script> </body> </html> </run> <blockquote>"use strict" 指令只允许出现在脚本或函数的开头。</blockquote>
返回至“
JavaScript 严格模式
”。
上一节:
JavaScript_变量提升
下一节:
JavaScript_使用误区