您的位置:首页 > Web前端 > JavaScript

JavaScript严格模式与非严格模式区别

2020-04-05 07:16 751 查看

开启严格模式方法

"use strict";
, 如果放在文件开头就是全局开启严格模式, 还可以在函数内声明, 这么做的话就是这个函数开启严格模式.

  1. 严格模式下无法再意外创建全局变量。在普通的JavaScript里面给一个拼写错误的变量名赋值会使全局对象新增一个属性。严格模式中意外创建全局变量被抛出错误替代:
"use strict";
a = 1; // 报错, 因为找不到a的声明
  1. 在严格模式下, 试图删除不可删除的属性时会抛出异常
"use strict";
delete Object.prototype; // 抛出TypeError错误, 如果不是在严格模式会返回false, 不会报错
  1. , 严格模式要求函数的参数名唯一. 在正常模式下, 最后一个重名参数名会掩盖之前的重名参数. 之前的参数仍然可以通过 arguments[i] 来访问, 还不是完全无法访问.
function sum(a, a, c){ // !!! 语法错误
"use strict";
return a + a + c; // 代码运行到这里会出错 Uncaught SyntaxError: Duplicate parameter name not allowed in this context
}
  1. 严格模式禁止八进制数字语法. 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.
  1. 严格模式禁用 with
  2. 严格模式下的 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
  1. 严格模式禁止删除声明变量.;
"use strict";

var x = 1;
delete x; // !!! 语法错误 Uncaught SyntaxError: Delete of an unqualified identifier in strict mode. 如果是非严格模式, 返回false
  1. 严格模式下,参数的值不会随 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
  1. 不再支持
    arguments.callee
    . 正常模式下,
    arguments.callee
    指向当前正在执行的函数.
  2. 在严格模式下通过
    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对象
  1. 在严格模式下,那么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

  • 点赞
  • 收藏
  • 分享
  • 文章举报
刘翾 博客专家 发布了167 篇原创文章 · 获赞 275 · 访问量 59万+ 他的留言板 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: