Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

浅谈 JS 严格模式 #5

Open
KarthusLorin opened this issue Jun 25, 2020 · 0 comments
Open

浅谈 JS 严格模式 #5

KarthusLorin opened this issue Jun 25, 2020 · 0 comments

Comments

@KarthusLorin
Copy link
Owner

浅谈 JS 严格模式

简介

何为严格模式?严格模式(strict mode)即在严格的条件下运行,在严格模式下,很多正常情况下不会报错的问题语句,将会报错并阻止运行。

但是,严格模式可以显著提高代码的健壮性,比如JS经常被人诟病的隐式创建全局变量,在严格模式下就会阻止运行。

总的来说,引入严格模式虽然会导致一些代码不可运行,但是,严格模式使得一些JS广受诟病的问题无法运行,从长期角度上看,绝对是利大于弊。

用法

进入严格模式的方法很简单,只需要在在需要进入严格模式的作用域开头输入"user strict"即可,值得一提的是,在无法执行严格模式的旧版浏览器中(IE10之前),该条指令会自动被忽略。

例1:

"user strict";
x = 1;			// Uncaught ReferenceError: x is not defined

如例1所示,在全局作用域的开头定义了严格模式,并隐式定义了全局变量xx = 1 这条语句在严格模式下,抛出了异常,提示没有显式创建变量a

例2:

"use strict";
fn();

function fn () {
  x = 1;			// Uncaught ReferenceError: x is not defined
}

在例2中,由于严格模式定义在全局作用域中,而fn作用域被全局作用域所包含,所以fn作用域中同样执行了严格模式。

注:也可以通过执行上下文栈来解释。

例3:

x = 1;
fn();

function fn () {
  "use strict"
  y = 2			// Uncaught ReferenceError: y is not defined
}

在例3中,由于严格模式只定义在fn函数的局部作用域中,在全局作用域中不起作用,所以全局作用域中,隐式定义全局变量x没有抛出异常,而在局部作用域中,隐式定义全局变量y抛出异常。

规则

1.严格模式下无法隐式创建全局变量

2.严格模式会使引起静默失败(silently fail,注:不报错也没有任何效果)的赋值操做抛出异常

"use strict";

// 给不可写属性赋值
var obj1 = {};
Object.defineProperty(obj1, "x", { value: 42, writable: false });
obj1.x = 9; // 抛出TypeError错误

// 给只读属性赋值
var obj2 = { get x() { return 17; } };
obj2.x = 5; // 抛出TypeError错误

// 给不可扩展对象的新属性赋值
var fixed = {};
Object.preventExtensions(fixed);
fixed.newProp = "ohai"; // 抛出TypeError错误

3.在严格模式下,试图删除不可删除的属性时,会抛出异常(之前这种操作不会产生任何效果)

"use strict";
delete Object.prototype; // 抛出TypeError错误

4.在严格模式下,不允许重名属性

"use strict";
var o = { p: 1, p: 2 }; // 语法错误

5.严格模式要求函数参数名唯一

function sum(a, a, c){ // 语法错误
  "use strict";
  return a + a + c; // 代码运行到这里会出错
}

6.禁止八进制数字语法

"use strict";
var sum = 015 + // 语法错误
          197 +
          142;

7.禁止设置原始类型(primitive)值的属性

(function() {
  "use strict";

  false.true = "";              //TypeError
  (14).sailing = "home";        //TypeError
  "with".you = "far away";      //TypeError

})();

8.禁用with

9.严格模式下,eval()创建变量不能被调用

"use strict";
eval ("var x = 2");
alert (x);               		// Uncaught ReferenceError: x is not defined

10.严格模式禁止删除声明变量

"use strict";

var x;
delete x; // 语法错误

eval("var y; delete y;"); // 语法错误

11.不能使用evalarguments字符串

"use strict";
var arguments = 1;    // Uncaught SyntaxError: Unexpected eval or arguments in strict mode
var eval = 2;		  // Uncaught SyntaxError: Unexpected eval or arguments in strict mode

12.严格模式下,函数的 arguments 对象会保存函数被调用时的原始参数。arguments[i] 的值不会随与之相应的参数的值的改变而变化,同名参数的值也不会随与之相应的 arguments[i] 的值的改变而变化。

13.不再支持arguments.callee

"use strict";
var f = function() { return arguments.callee; };
f(); 				// TypeError

14.保留部分关键字,这些字符包括implements, interface, let, package, private, protected, public, staticyield。在严格模式下,你不能再用这些名字作为变量名或形参名。

15.禁止this指向全局对象,当this指向全局对象时,自动转为undefined

总结

随着JS的飞速发展,出现了一大堆可以代替严格模式的工具,比如eslint等,但是,当你想要提升原生JS代码的健壮性和可读性,回避JS一些被人诟病的语法,严格模式是你不二的选择。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant