- 使用指令”use strist”
- 为什么使用严格模式
- 消除js语法不合理,不严谨保证代码运行安全
- 提高编译器效率,提高速度
- 为新版本js做铺垫
- 体现了合理安全严格的发展方向,包括IE10主流的浏览器的支持
- 指令出现位置 ```vue
```vue<script>function foo(){//此方法以严格模式执行"use strict";}</script>
1.在严格模式下删除对象属性需要configurable置为true
<script>"use strict";var o = Object.create(null,{'x':{value:1234,configurable:true}})console.log(o.x); //1234delete o.xconsole.log(o.x); //undefind</script>
严格模式下,不能使用未声明的变量
<script>"use strict"x = 123;console.log(123); //报错 x in not defind</script>
- 严格模式下,不允许参数名相同
- 严格模式下,不允许是八进制
- 严格模式下,不允许使用转义字符
严格模式下,不允许只对只读属性赋值
<script>"use strict";var obj = {};Object.defineProperty(obj, "x", {value: 0,writable: false});obj.x = 3.14;//Uncaught TypeError: Cannot assign to read only property 'x' of object '#<Object>'</script>
1.严格模式下,不允许对一个使用getter方法读取的属性进行赋值
<script>"use strict";var obj = {get x() {return 0}};obj.x = 3.14;//Uncaught TypeError: Cannot set property x of #<Object> which has only a getter无法设置只有getter的对象>的属性x</script>
严格模式下,不允许删除一个不允许删除的属性
<script>"use strict";delete Object.prototype;//Uncaught TypeError: Cannot delete property 'prototype' of function Object() { [native code] }(不能删除函数对象()的属性“原型”{ [本机代码] })</script>
严格模式下,变量名不能使用 “eval” 字符串
- 严格模式下,变量名不能使用 “arguments” 字符串
严格模式下,不允许使用以下这种语句
<script>"use strict"with(Math) {x = cos(2)};//Uncaught SyntaxError: Strict mode code may not include a with statement</script>
严格模式下,由于一些安全原因,在作用域 eval() 创建的变量不能被调用
<script>"use strict"eval ("var x = 2");alert (x); //Uncaught ReferenceError: x is not defined</script>
