判断当前的JavaScript是否开启了“use strict”模式,JavaScript check if strict mode is enforced?

使用以下代码可以判断是否开启了strict模式

var isStrict = (function () { return !this; })();
console.log(isStrict);

因为在strict模式下,上面函数里的this是undefined
而在非strict模式下,上面函数里的this是Window(如果在浏览器里运行)或者其他运行环境,比如node等

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注