You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"use strict";vararguments=1;// Uncaught SyntaxError: Unexpected eval or arguments in strict modevareval=2;// Uncaught SyntaxError: Unexpected eval or arguments in strict mode
浅谈 JS 严格模式
简介
何为严格模式?严格模式(strict mode)即在严格的条件下运行,在严格模式下,很多正常情况下不会报错的问题语句,将会报错并阻止运行。
但是,严格模式可以显著提高代码的健壮性,比如JS经常被人诟病的隐式创建全局变量,在严格模式下就会阻止运行。
总的来说,引入严格模式虽然会导致一些代码不可运行,但是,严格模式使得一些JS广受诟病的问题无法运行,从长期角度上看,绝对是利大于弊。
用法
进入严格模式的方法很简单,只需要在在需要进入严格模式的作用域开头输入
"user strict"
即可,值得一提的是,在无法执行严格模式的旧版浏览器中(IE10之前),该条指令会自动被忽略。例1:
如例1所示,在全局作用域的开头定义了严格模式,并隐式定义了全局变量
x
,x = 1
这条语句在严格模式下,抛出了异常,提示没有显式创建变量a
。例2:
在例2中,由于严格模式定义在全局作用域中,而
fn
作用域被全局作用域所包含,所以fn
作用域中同样执行了严格模式。注:也可以通过执行上下文栈来解释。
例3:
在例3中,由于严格模式只定义在
fn
函数的局部作用域中,在全局作用域中不起作用,所以全局作用域中,隐式定义全局变量x
没有抛出异常,而在局部作用域中,隐式定义全局变量y
抛出异常。规则
1.严格模式下无法隐式创建全局变量
2.严格模式会使引起静默失败(silently fail,注:不报错也没有任何效果)的赋值操做抛出异常
3.在严格模式下,试图删除不可删除的属性时,会抛出异常(之前这种操作不会产生任何效果)
4.在严格模式下,不允许重名属性
5.严格模式要求函数参数名唯一
6.禁止八进制数字语法
7.禁止设置原始类型(primitive)值的属性
8.禁用
with
9.严格模式下,
eval()
创建变量不能被调用10.严格模式禁止删除声明变量
11.不能使用
eval
和arguments
字符串12.严格模式下,函数的 arguments 对象会保存函数被调用时的原始参数。arguments[i] 的值不会随与之相应的参数的值的改变而变化,同名参数的值也不会随与之相应的 arguments[i] 的值的改变而变化。
13.不再支持
arguments.callee
14.保留部分关键字,这些字符包括
implements
,interface
,let
,package
,private
,protected
,public
,static
和yield
。在严格模式下,你不能再用这些名字作为变量名或形参名。15.禁止
this
指向全局对象,当this
指向全局对象时,自动转为undefined
总结
随着JS的飞速发展,出现了一大堆可以代替严格模式的工具,比如
eslint
等,但是,当你想要提升原生JS代码的健壮性和可读性,回避JS一些被人诟病的语法,严格模式是你不二的选择。The text was updated successfully, but these errors were encountered: