JavaScript严格模式
"use strict"; 定义JavaScript代码应该在严格模式下执行。 "use strict" 指令 "use strict" 是在JavaScript 1.8.5(ECMAScript 5版)中的新指令。 它不是一个语句,是字面量表达式,早期版本的JavaScript忽略. "use strict"的目的是指示代码应以“严格模式”执行 以严格的方式,你不可以,例如,使用未声明的变量. 严格模式支持的浏览器有: IE from version 10. Firefox from version 4. Chrome from version 13. Safari from version 5.1. Opera from version 12. 声明严格模式 严格模式是通过添加"use strict";到脚本或函数的开头 在脚本开始时声明,它具有全局作用域(脚本中的所有代码将严格执行): "use strict"; x = 3.14; // This will cause an error because x is not declared 让我试试 "use strict"; myFunction(); function myFunction() { y = 3.14; // This will also cause an error because y is not declared } 让我试试 在函数中声明,它具有局部作用域(只有函数内的代码处于严格模式): x = 3.14; // This will not cause an error. myFunction(); function myFunction() { "use strict"; y = 3.14; // This will cause an error } 让我试试 "use strict"; 语法 语法,声明严格的模式,被设计成与旧版本的JavaScript兼容. 编写一个数字文字(4 + 5;)或一个字符串字面值("John Doe";)在JavaScript程序有没有副作用。它简单地编译为一个不存在变量。 因此 "use strict"; 只有对新的编译器“理解”它的意义. 为什么使用严格模式? 严格的模式,使得它更容易写“安全”的JavaScript. 严格的模式改变以前接受的“
"use strict"; 定义JavaScript代码应该在严格模式下执行。
"use strict" 指令
"use strict" 是在JavaScript 1.8.5(ECMAScript 5版)中的新指令。
它不是一个语句,是字面量表达式,早期版本的JavaScript忽略.
"use strict"的目的是指示代码应以“严格模式”执行
以严格的方式,你不可以,例如,使用未声明的变量.
严格模式支持的浏览器有: IE from version 10. Firefox from version 4. Chrome from version 13. Safari from version 5.1. Opera from version 12.
声明严格模式
严格模式是通过添加"use strict";到脚本或函数的开头
在脚本开始时声明,它具有全局作用域(脚本中的所有代码将严格执行):
"use strict"; x = 3.14; // This will cause an error because x is not declared
"use strict"; myFunction(); function myFunction() { y = 3.14; // This will also cause an error because y is not declared }
在函数中声明,它具有局部作用域(只有函数内的代码处于严格模式):
x = 3.14; // This will not cause an error. myFunction(); function myFunction() { "use strict"; y = 3.14; // This will cause an error }
"use strict"; 语法
语法,声明严格的模式,被设计成与旧版本的JavaScript兼容.
编写一个数字文字(4 + 5;)或一个字符串字面值("John Doe";)在JavaScript程序有没有副作用。它简单地编译为一个不存在变量。
因此 "use strict"; 只有对新的编译器“理解”它的意义.
为什么使用严格模式?
严格的模式,使得它更容易写“安全”的JavaScript.
严格的模式改变以前接受的“错误的语法”到真正的错误.
例如,在正常的JavaScript,输入变量名创建一个新的全局变量。在严格的模式下,这将抛出一个错误,使不可能意外地创建一个全局变量.
在正常的JavaScript,开发者将不会收到任何错误反馈不可写的属性分配值.
严格模式,任何分配给非可写属性,吸气剂的唯一财产,一个不存在的属性,一个不存在的变量,或一个不存在的对象,会抛出一个错误。.
允许在严格模式下
使用变量,不声明它,是不允许的:
"use strict"; x = 3.14; // This will cause an error
对象也是变量。
使用对象,而不声明它,是不允许的:
"use strict"; x = {p1:10, p2:20}; // This will cause an error
不允许删除变量(或对象).
"use strict"; var x = 3.14; delete x; // This will cause an error
不允许删除函数.
"use strict"; function x(p1, p2) {}; delete x; // This will cause an error
复制一个参数名是不允许的:
"use strict"; function x(p1, p1) {}; // This will cause an error
八进制数字字面值是不允许的:
"use strict"; var x = 010; // This will cause an error
不允许转义字符:
"use strict"; var x = \010; // This will cause an error
不允许写入只读属性:
"use strict"; var obj = {}; Object.defineProperty(obj, "x", {value:0, writable:false}); obj.x = 3.14; // This will cause an error
不允许写入只获取属性:
"use strict"; var obj = {get x() {return 0} }; obj.x = 3.14; // This will cause an error
删除一个不可删除的属性是不允许的:
"use strict"; delete Object.prototype; // This will cause an error
字符串"eval"不能作为变量使用:
"use strict"; var eval = 3.14; // This will cause an error
字符串"arguments"不能作为变量使用:
"use strict"; var arguments = 3.14; // This will cause an error
with语句不允许:
"use strict"; with (Math){x = cos(2)}; // This will cause an error
出于安全原因, eval() 不允许在其被调用的范围内创建变量:
"use strict"; eval ("var x = 2"); alert (x); // This will cause an error
在函数调用像f(),这个值是全局对象。在严格的模式下,它现在是未定义的.
未来的证明!
未来保留关键字不允许在严格模式下。这些有:
- implements
- interface
- let
- package
- private
- protected
- public
- static
- yield
"use strict"; var public = 1500; // This will cause an error // This will cause an error
看见了吧!"use strict" 指令仅在脚本或函数的开头被识别.
交流与评论