简介

我们都知道javascript是一个弱类型语言,在ES5之前,javascript的程序编写具有很强的随意性,我可以称之为懒散模式(sloppy mode)。比如可以使用未定义的变量,可以给对象中的任意属性赋值并不会抛出异常等等。

在ES5中,引入了strict模式,我们可以称之为严格模式。相应的sloppy mode就可以被称为非严格模式。

严格模式并不是非严格模式的一个子集,相反的严格模式在语义上和非严格模式都发生了一定的变化,所以我们在使用过程中,一定要经过严格的测试。以保证在严格模式下程序的执行和非严格模式下的执行效果一致。

使用Strict mode

strict mode会改变javascript的一些表现,我们将会在下一节中进行详细的讲解。

这里先来看一下,怎么使用strict mode。

Strict mode主要用在一个完整的脚本或者function中,并不适用于block {}。 如果在block中使用strict mode是不会生效的。

除此之外,eval中的代码,Function代码,event handler属性和传递给WindowTimers.setTimeout()的string都可以看做是一个完整的脚本。我们可以在其中使用Strict mode。

如果是在script脚本中使用strict模式,可以直接在脚本的最上面加上"use strict":

// 整个脚本的strict模式
'use strict';
var v = "Hi! I'm a strict mode script!";

同样的我们也可以在function中使用strict模式:

function strict() {
// 函数的strict模式
'use strict';
function nested() { return 'And so am I!'; }
return "Hi! I'm a strict mode function! " + nested();
}
function notStrict() { return "I'm not strict."; }

如果使用的是ES6中引入的modules,那么modules中默认就已经是strict模式了,我们不需要再额外的使用"use strict":

function myModule() {
// 默认就是strict模式
}
export default myModule;

strict mode的新特性

strict mode在语法和运行时的表现上面和非严格模式都发生了一定的变化,接下来,我们一一来看。

强制抛出异常

在js中,有很多情况下本来可能是错误的操作,但是因为语言特性的原因,并没有抛出异常,从而导致最终运行结果并不是所期待的。

如果使用strict模式,则会直接抛出异常。

比如在strict模式中,不允许使用未定义的全局变量:

'use strict';

globalVar = 10; //ReferenceError: globalVar is not defined

这样实际上可以避免手误导致变量名字写错而导致的问题。

我再看一些其他的例子:

'use strict';

// 赋值给不可写的全局变量,
var undefined = 5; // throws a TypeError
var Infinity = 5; // throws a TypeError // 赋值给不可写的属性
var obj1 = {};
Object.defineProperty(obj1, 'x', { value: 42, writable: false });
obj1.x = 9; // throws a TypeError // 赋值给一个get方法
var obj2 = { get x() { return 17; } };
obj2.x = 5; // throws a TypeError // 赋值给一个禁止扩展的对象
var fixed = {};
Object.preventExtensions(fixed);
fixed.newProp = 'ohai'; // throws a TypeError

Strict模式可以限制删除不可删除的属性,比如构造函数的prototype:

'use strict';
delete Object.prototype; // throws a TypeError

禁止对象和函数参数中的重复属性:

'use strict';
var o = { p: 1, p: 2 }; // Duplicate declaration function sum(a, a, c) { // Duplicate declaration
'use strict';
return a + a + c;
}

禁止设置基础类型的属性:

(function() {
'use strict'; false.true = ''; // TypeError
(14).sailing = 'home'; // TypeError
'with'.you = 'far away'; // TypeError })();

简化变量的使用

使用Strict模式可以简化变量的使用,让程序代码可读性更强。

首先,strict模式禁止使用with。

with很强大,我们可以通过将对象传递给with,从而影响变量查找的scope chain。也就是说当我们在with block中需要使用到某个属性的时候,除了在现有的scope chain中查找之外,还会在with传递的对象中查找。

with (expression)
statement

使用with通常是为了简化我们的代码,比如:

var a, x, y;
var r = 10; with (Math) {
a = PI * r * r;
x = r * cos(PI);
y = r * sin(PI / 2);
}

上面的例子中,PI是Math对象中的变量,但是我们可以在with block中直接使用。有点像java中的import的感觉。

下面的例子将会展示with在使用中的问题:

function f(x, o) {
with (o) {
console.log(x);
}
}

我们在with block中输出x变量,从代码可以看出f函数传入了一个x变量。但是如果with使用的对象中如果也存在x属性的话,就会出现意想不到的问题。

所以,在strict模式中,with是禁止使用的。

其次是对eval的改动。

传统模式中,eval中定义的变量,将会自动被加入到包含eval的scope中。我们看个例子:

var x = 17;
var evalX = eval("var x = 42; x;");
console.log(x);

因为eval中引入了新的变量x,这个x的值将会覆盖最开始定义的x=17. 最后我们得到结果是42.

如果加入use strict,eval中的变量将不会被加入到现有的Scope范围中,我们将会得到结果17.

var x = 17;
var evalX = eval("'use strict'; var x = 42; x;");
console.log(x);

这样做的好处是为了避免eval对现有程序逻辑的影响。

在strict模式下面,还不允许delete name:

'use strict';

var x;
delete x; // !!! syntax error eval('var y; delete y;'); // !!! syntax error~~

简化arguments

在js中,arguments代表的是参数数组,首先在Strict模式下,arguments是不能作为变量名被赋值的:

'use strict';
arguments++;
var obj = { set p(arguments) { } };
try { } catch (arguments) { }
function arguments() { }
var f = new Function('arguments', "'use strict'; return 17;");

上面执行都会报错。

另外,在普通模式下,arguments是和命名参数相绑定的,并且arguments[0]和arg同步变化,都表示的是第一个参数。

但是如果在strict模式下,arguments表示的是真正传入的参数。

我们举个例子:

function f(a) {
a = 42;
return [a, arguments[0]];
}
var pair = f(17);
console.log(pair[0]); // 42
console.log(pair[1]); // 42

上面的例子中,arguments[0]是和命名参数a绑定的,不管f传入的是什么值,arguments[0]的值最后都是42.

如果换成strict模式:

function f(a) {
'use strict';
a = 42;
return [a, arguments[0]];
}
var pair = f(17);
console.log(pair[0]); // 42
console.log(pair[1]); // 17

这个模式下arguments[0]接收的是实际传入的参数,我们得到结果17.

在Strict模式下,arguments.callee是被禁用的。通常来说arguments.callee指向的是当前执行的函数,这会阻止虚拟机对内联的优化,所以在Strict模式下是禁止的。

让javascript变得更加安全

在普通模式下,如果我们在一个函数f()中调用this,那么this指向的是全局对象。在strict模式下,这个this的值是undefined。

如果我们是通过call或者apply来调用的话,如果传入的是primitive value(基础类型),在普通模式下this会自动指向其box类(基础类型对应的Object类型,比如Boolean,Number等等)。如果传入的是undefined和null,那么this指向的是global Object。

而在strict模式下,this指向的是传入的值,并不会做转换或变形。

下面的值都是true:

'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);

为什么会安全呢?这就意味着,在strict模式下,不能通过this来指向window对象,从而保证程序的安全性。

另外,在普通模式下,我们可以通过fun.caller或者fun.arguments来获取到函数的调用者和参数,这有可能会访问到一些private属性或者不安全的变量,从而造成安全问题。

在strict模式下,fun.caller或者fun.arguments是禁止的。

function restricted() {
'use strict';
restricted.caller; // throws a TypeError
restricted.arguments; // throws a TypeError
}
function privilegedInvoker() {
return restricted();
}
privilegedInvoker();

保留关键字和function的位置

为了保证JS标准的后续发展,在strict模式中,不允许使用关键字作为变量名,这些关键字包括implements, interface, let, package, private, protected, public, static 和 yield等。

function package(protected) { // !!!
'use strict';
var implements; // !!! interface: // !!!
while (true) {
break interface; // !!!
} function private() { } // !!!
}
function fun(static) { 'use strict'; } // !!!

而对于function来说,在普通模式下,function是可以在任何位置的,在strict模式下,function的定义只能在脚本的顶层或者function内部定义:


'use strict';
if (true) {
function f() { } // !!! syntax error
f();
} for (var i = 0; i < 5; i++) {
function f2() { } // !!! syntax error
f2();
} function baz() { // kosher
function eit() { } // also kosher
}

总结

Strict模式为JS的后续发展和现有编程模式的规范都起到了非常重要的作用。但是如果我们在浏览器端使用的话,还是需要注意浏览器的兼容性,并做好严格的测试。

本文作者:flydean程序那些事

本文链接:http://www.flydean.com/js-use-strict/

本文来源:flydean的博客

欢迎关注我的公众号:「程序那些事」最通俗的解读,最深刻的干货,最简洁的教程,众多你不知道的小技巧等你来发现!

javascript中的Strict模式的更多相关文章

  1. 理解javascript中的策略模式

    理解javascript中的策略模式 策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换. 使用策略模式的优点如下: 优点:1. 策略模式利用组合,委托等技术和思想,有效 ...

  2. 浅谈 JavaScript 中的继承模式

    最近在读一本设计模式的书,书中的开头部分就讲了一下 JavaScript 中的继承,阅读之后写下了这篇博客作为笔记.毕竟好记性不如烂笔头. JavaScript 是一门面向对象的语言,但是 ES6 之 ...

  3. javaScript中的严格模式 (译)

    “use strict”状态指示浏览器使用严格模式,是javaScript中一个相对少且安全的特征集. 特征列表(非完全列举) 不允许定义全局变量.(捕获没有用var声明的变量和变量名的拼写错误) 在 ...

  4. 理解javascript中的原型模式

    一.为什么要用原型模式. 早期采用工厂模式或构造函数模式的缺点:  1.工厂模式:函数creatPerson根据接受的参数来构建一个包含所有必要信息的person对象,这个函数可以被无数次的调用,工厂 ...

  5. 浅谈JavaScript中的原型模式

    在JavaScript中创建对象由很多种方式,如工厂模式.构造函数模式.原型模式等: <pre name="code" class="html">/ ...

  6. 【JS】336- 拆解 JavaScript 中的异步模式

    点击上方"前端自习课"关注,学习起来~ JavaScript 中有很多种异步编程的方式.callback.promise.generator.async await 甚至 RxJS ...

  7. 【JS】285- 拆解 JavaScript 中的异步模式

    JavaScript 中有很多种异步编程的方式.callback.promise.generator.async await 甚至 RxJS.我最初接触不同的异步模式时,曾想当然的觉得 promise ...

  8. Javascript 中的严格模式

    原文:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 一.概述 除了正常运行模式,ECMAscript 5添加了第 ...

  9. JavaScript中的继承模式总结

    一.总结: //js中的几种继承 //原型链的问题,包含引用类型的原型属性会被实例共享,子类型无法给超类型传递参数 function SuperType() { this.colors = [&quo ...

随机推荐

  1. 洛谷p1637 三元上升子序列(树状数组

    题目描述 Erwin最近对一种叫"thair"的东西巨感兴趣... 在含有n个整数的序列a1,a2......an中, 三个数被称作"thair"当且仅当i&l ...

  2. 007.NET5 Log4Net组件使用

    NET 5 Log4Net组件使用 1. Nuget引入程序集:log4net + Microsfot.Extensions.Logging.Log4Net.AspNetCore 2. 准备配置文件 ...

  3. App icons generator

    App icons generator https://appicon.co/ Drag or select an app icon image (1024x1024) to generate dif ...

  4. ReactDOM API All In One

    ReactDOM API All In One React DOM API render() hydrate() unmountComponentAtNode() findDOMNode() crea ...

  5. Promise nested then execute order All In One

    Promise nested then execute order All In One Promise nested then nested Promise not return new Promi ...

  6. wifi IP address scanner on macOS

    wifi IP address scanner on macOS Nmap Network Scanning https://nmap.org/book/inst-macosx.html https: ...

  7. illustrating javascript prototype & prototype chain

    illustrating javascript prototype & prototype chain 图解 js 原型和原型链 proto & prototype func; // ...

  8. npm init & npx create & yarn create

    npm init & npx create & yarn create https://create-react-app.dev/docs/getting-started/#creat ...

  9. Techme Inc热心公益事业 积极开展公益活动

    从2015年起,Techme inc(公司编号:20151524696)便通过优质的产品和服务,帮助顾客实现营养与健康的目标.与此同时,Techme inc(公司编号:20151524696)多年来始 ...

  10. spring扩展点整理

    本文转载自spring扩展点整理 背景 Spring的强大和灵活性不用再强调了.而灵活性就是通过一系列的扩展点来实现的,这些扩展点给应用程序提供了参与Spring容器创建的过程,好多定制化的东西都需要 ...