JavaScript严格模式与非严格模式区别
2020-04-05 07:16
751 查看
开启严格模式方法
"use strict";, 如果放在文件开头就是全局开启严格模式, 还可以在函数内声明, 这么做的话就是这个函数开启严格模式.
- 严格模式下无法再意外创建全局变量。在普通的JavaScript里面给一个拼写错误的变量名赋值会使全局对象新增一个属性。严格模式中意外创建全局变量被抛出错误替代:
"use strict"; a = 1; // 报错, 因为找不到a的声明
- 在严格模式下, 试图删除不可删除的属性时会抛出异常
"use strict"; delete Object.prototype; // 抛出TypeError错误, 如果不是在严格模式会返回false, 不会报错
- , 严格模式要求函数的参数名唯一. 在正常模式下, 最后一个重名参数名会掩盖之前的重名参数. 之前的参数仍然可以通过 arguments[i] 来访问, 还不是完全无法访问.
function sum(a, a, c){ // !!! 语法错误 "use strict"; return a + a + c; // 代码运行到这里会出错 Uncaught SyntaxError: Duplicate parameter name not allowed in this context }
- 严格模式禁止八进制数字语法. ECMAScript并不包含八进制语法, 但所有的浏览器都支持这种以零(0)开头的八进制语法:
0644 === 420
还有"\045" === "%"
.在ECMAScript 6中支持为一个数字加"0o"的前缀来表示八进制数.
var a = 0o10; // ES6: 八进制 ------------------ "use strict"; var a = 015 + // !!! 语法错误 Uncaught SyntaxError: Octal literals are not allowed in strict mode.
- 严格模式禁用 with
- 严格模式下的 eval 不再为上层范围(surrounding scope,注:包围eval代码块的范围)引入新变量.
var x = 1; eval("var x = 2"); console.log(x); // 2 --------------- var x = 1; eval(" 'use strict'; var x = 2") console.log(x); // 1
- 严格模式禁止删除声明变量.;
"use strict"; var x = 1; delete x; // !!! 语法错误 Uncaught SyntaxError: Delete of an unqualified identifier in strict mode. 如果是非严格模式, 返回false
- 严格模式下,参数的值不会随 arguments 对象的值的改变而变化. 严格模式下,函数的 arguments 对象会保存函数被调用时的原始参数.
function f(a){ a = 42; console.log(a, arguments[0]); } var pair = f(17); // 42 42 ---------- function f(a){ "use strict"; a = 42; console.log(a, arguments[0]); } var pair = f(17); // 42 17
- 不再支持
arguments.callee
. 正常模式下,arguments.callee
指向当前正在执行的函数. - 在严格模式下通过
this
传递给一个函数的值不会被强制转换为一个对象。对一个普通的函数来说,this
总会是一个对象:不管调用时this
它本来就是一个对象;还是用布尔值,字符串或者数字调用函数时函数里面被封装成对象的this
;还是使用undefined
或者null
调用函数式this
代表的全局对象(使用call
,apply
或者bind
方法来指定一个确定的this
) 对于一个开启严格模式的函数,指定的this
不再被封装为对象,而且如果没有指定this
的话它值是undefined
;
"use strict"; function fun() { return this; } console.assert(fun() === undefined); console.assert(fun.call(2) === 2); console.assert(fun.apply(null) === null); console.assert(fun.call(undefined) === undefined); console.assert(fun.bind(true)() === true); ----------------- function fun() { return this; } console.log(fun()); // window对象 console.log(fun.call(2)); // Number(2) 封箱的数字2 console.log(fun.apply(null)); // window对象 console.log(fun.call(undefined)); // window对象 console.log(fun.bind(true)()); // Boolean对象
- 在严格模式下,那么fun.caller和fun.arguments都是不可删除的属性而且在存值、取值时都会报错;
function restricted() { "use strict"; restricted.caller; // 抛出类型错误 Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them restricted.arguments; // 抛出类型错误 } function privilegedInvoker() { return restricted(); } privilegedInvoker(); ------------ function restricted() { restricted.caller; // 返回privilegedInvoker函数 restricted.arguments; // 返回arguments } function privilegedInvoker() { return restricted(); } privilegedInvoker();
参考链接
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Strict_mode
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- JavaScript严格模式与非严格模式的区别
- JavaScript严格模式与非严格模式之间的区别
- javascript权威指南 严格模式与非严格模式的一些区别
- JavaScript严格模式下this指向
- 理解javascript中的严格模式
- Javascript 严格模式详解
- JavaScript严格模式详解
- Js中严格模式和非严格模式的区别
- 浅谈JavaScript严格模式
- Javascript 严格模式详解
- 网络嗅探混杂模式与非混杂模式的区别
- JavaScript 严格模式 use strict
- JavaScript之严格模式 "use strict";
- javascript中的严格模式
- JavaScript 严格模式
- Javascript 严格模式use strict详解
- JavaScript强化教程——严格模式
- Javascript的严格模式
- javascript严格模式下的8点规则
- JavaScript和angularJs语法支持严格模式:”use strict”