摘要:

  ECMAScript5中引入的严格模式,通过让JavaScript运行环境对一些开发过程中最常见和不易发现的错误做出和当前不同的处理,来让开发者拥有一个”更好”的JavaScript语言。但目前为止,所有主流的浏览器都在他们的高版本中支持了严格模式,包括IE10、Firefox4、chrome12、Opera12、Android4和IOS5。

  严格模式是一个更好的方法引入检查错误代码。使用严格的模式时,您不能,例如,使用隐式声明变量,给只读属性赋值,或将属性添加到一个不可扩展的对象中。

声明严格模式:

  你可以宣布严格模式通过添加“use strict”;在一个文件、一个程序或一个函数的开始。这种声明称为指令序言。严格模式声明的范围取决于它的上下文。如果它是宣布在全局上下文(一个函数的范围之外),程序的所有代码在严格的模式下执行。如果在一个函数内声明它,则函数内所有的代码在严格模式下执行。例如,在下面的示例中所有的代码都是在严格模式下,函数外声明的变量导致语法错误“变量未定义的严格模式”。 

 "use strict";
​function testFunction(){
var testvar = 4;
return testvar;
}
// This causes a syntax error.
testvar = 5;

  在接下来的例子中,只有testFunction里面的代码在严格模式下执行。函数外的变量未声明不会导致一个语法错误,但在函数内会导致语法错误。

 function testFunction(){
"use strict";
// This causes a syntax error.
testvar = 4;
return testvar;
}
testvar = 5;

注意:

  • 如果浏览器不支持严格模式会忽略"use strict"这个字符串。这样就允许跨浏览器的使用严格模式语法,这是为了保证向前兼容,防止有一天某些浏览器仅仅支持严格模式。测试你的浏览器是否支持严格模式
  • 当严格模式的方法调用了一个非严格方法时,该非严格方法不会启用严格模式,因为非严格方法被当作参数传递或是通过call和apply调用
  • 当严格模式的方法调用了一个非严格方法时,该非严格方法不会启用严格模式,因为非严格方法被当作参数传递或是通过call和apply调用

严格模式的利与弊:

  • 优点:
    提高编译器效率,增加运行速度
    消除Javascript语法的一些不合理、不严谨之处,减少一些怪异行为
    消除代码运行的一些不安全之处,保证代码运行的安全
  • 缺点:
    在"严格模式"中,同样的代码可能会有不一样的运行结果
    ​一些在"正常模式"下可以运行的语句,在"严格模式"下将不能运行

严格模式的限制:

  下表列出了在严格模式最重要的限制

​Language element

Restriction

Error

Example

Variable

Using a variable without declaring it.

SCRIPT5042: Variable undefined in strict mode

JavaScript

testvar = 4;

Read-only property

Writing to a read-only property.

SCRIPT5045: Assignment to read-only properties is not allowed in strict mode

JavaScript

var testObj = Object.defineProperties({}, {
prop1: {
value: 10,
writable: false // by default
},
prop2: {
get: function () {
}
}
});
testObj.prop1 = 20;
testObj.prop2 = 30;

Non-extensible property

Adding a property to an object whoseextensibleattribute is set to false.

SCRIPT5046: Cannot create property for a non-extensible object

JavaScript

var testObj = new Object();

Object.preventExtensions(testObj);

testObj.name = "Bob";

delete

Deleting a variable, a function, or an argument.

Deleting a property whoseconfigurableattribute is set to false.

SCRIPT1045: Calling delete on <expression>is not allowed in strict mode

JavaScript

var testvar = 15;function testFunc() {};delete testvar;delete testFunc;

Object.defineProperty(testObj, "testvar", {
value: 10,
configurable: false
});delete testObj.testvar;

Duplicating a property

Defining a property more than once in an object literal.

SCRIPT1046: Multiple definitions of a property not allowed in strict mode

JavaScript

var testObj = {
prop1: 10,
prop2: 15,
prop1: 20
};

Duplicating a parameter name

Using a parameter name more than once in a function.

SCRIPT1038: Duplicate formal parameter names not allowed in strict mode

JavaScript

function testFunc(param1, param1) {    return 1;
};

Future reserved keywords

Using a future reserved keyword as a variable or function name.

SCRIPT1050: The use of a future reserved word for an identifier is invalid. The identifier name is reserved in strict mode.

  • implements

  • interface

  • package

  • private

  • protected

  • public

  • static

  • yield

Octals

Assigning an octal value to a numeric literal, or attempting to use an escape on an octal value.

SCRIPT1039: Octal numeric literals and escape characters not allowed in strict mode

JavaScript

var testoctal = 010;var testescape = \010;

this

The value ofthis is not converted to the global object when it is null orundefined.

 

JavaScript

function testFunc() {
return this;
}
var testvar = testFunc();

In non-strict mode, the value of testvar is the global object, but in strict mode the value is undefined.

evalas an identifier

The string "eval" cannot be used as an identifier (variable or function name, parameter name, and so on).

 

JavaScript

var eval = 10;

Function declared inside a statement or a block

You cannot declare a function inside a statement or a block.

SCRIPT1047: In strict mode, function declarations cannot be nested inside a statement or block. They may only appear at the top level or directly inside a function body.

JScript

var arr = [1, 2, 3, 4, 5];
var index = null;
for (index in arr) {
function myFunc() {};
}

Variable declared inside an evalfunction

If a variable is declared inside anevalfunction, it cannot be used outside that function.

SCRIPT1041: Invalid usage of 'eval' in strict mode

JavaScript

eval("var testvar = 10");
testvar = 15;

Indirect evaluation is possible, but you still cannot use a variable declared outside the eval function.

JavaScript

var indirectEval = eval;
indirectEval("var testvar = 10;");
document.write(testVar);

This code causes an error SCRIPT5009: 'testVar' is undefined.

Argumentsas an identifier

The string "arguments" cannot be used as an identifier (variable or function name, parameter name, and so on).

SCRIPT1042: Invalid usage of 'arguments' in strict mode

JavaScript

var arguments = 10;

argumentsinside a function

You cannot change the values of members of the localargumentsobject.

 

JavaScript

function testArgs(oneArg) {
arguments[0] = 20;
}

In non-strict mode, you can change the value of the oneArgparameter by changing the value of arguments[0], so that the value of both oneArg and arguments[0] is 20. In strict mode, changing the value of arguments[0] does not affect the value of oneArg, because the arguments object is merely a local copy.

arguments.callee

Not allowed.

 

JavaScript

function (testInt) {
if (testInt-- == 0)
return;
arguments.callee(testInt--);
}

with

Not allowed.

SCRIPT1037: 'with' statements are not allowed in strict mode

JavaScript

with (Math){
x = cos(3);
y = tan(7);
}

Strict Mode (JavaScript)的更多相关文章

  1. Javascript 严格模式("use strict";)详细解解

    1 1 1 Javascript 严格模式("use strict";)详细解解 "use strict";定义JavaScript代码应该在"str ...

  2. 初始JavaScript

    本文是笔者在看廖雪峰老师的JavaScript教程时的总结 一.加载 JavaScript           1.直接在html语句中写入JavaScript语句           2.在html ...

  3. JavaScript学习基础篇【第1篇】: JavaScript 入门

    JavaScript 快速入门 JavaScript代码可以直接嵌在网页的任何地方,不过通常我们都把JavaScript代码放到<head>中,由<script>...< ...

  4. javascript之标识(zhi)符、关键字与保留字

    正确区分标识(zhi)符.关键字与保留字 我发现很多初学者往往弄不清楚这三者的区别,甚至会把标识符的“识(zhi)”读作识(shi),真是愧对小学的语文老师啊!!! 注意:在JavaScript中,所 ...

  5. 为什么使用"use strict"可以节约你的时间

    转: http://ourjs.com/detail/52f572bf4534c0d806000024 "use strict"是JavaScript中一个非常好的特性,而且非常容 ...

  6. javascript变量 数组 对象

    一 变量 1.全局变量和局部变量 在JavaScript中同一个变量可以反复赋值,而且可以是不同类型的变量,但是要注意只能用var声明一次.这种变量类型不固定的语言称为动态语言,与之对应的静态语言,如 ...

  7. JavaScript 比较操作符,严格比较===

    JavaScript 有两种比较方式:严格比较运算符和转换类型比较运算符.对于严格比较运算符(三个 =)来说,为ture的情况是仅当两个操作数拥有相同的类型,而对于被广泛使用的比较运算符(两个 =)来 ...

  8. JavaScript学习笔记(一)——数据类型和变量

    在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...

  9. JavaScript Transpilers: 为什么需要用它们?Babel的使用介绍。

    英文原文 https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them 摘译(文章内的代码有些过期 ...

随机推荐

  1. [文件]Linux文本处理常用命令总结

    转自:https://www.cnblogs.com/sheeva/p/6406285.html 引子 作为一个偏爱windows的程序员,以前做文本处理的时候总是喜欢在windows下用notepa ...

  2. Azkaban上传出错

    Error initializing project id: 4 version: 2 打包文件上传时,自动生成的中文名导致的,这里的压缩包不能用中文名称.

  3. IIS连接数修改

    1, 对于xp等非服务器操作系统,IIS的并发数量是有限的.在xp上同时允许的连接数是10. 当程序出现性能问题时,可以通过下面的方法来查看并发连接数 这个工具在计算机管理里面可以找到 在图表区域,右 ...

  4. Intellij IDEA 将工程转换成maven工程 详解

    1> 右键工程,点击 Add Framework Support   2> 选中 Maven,再点击 OK   3> 工程根目录自动生成 pom.xml 文件,这样 工程就支持 Ma ...

  5. LibGDX skins

    https://github.com/czyzby/gdx-skins ———————————————————————————————————————————— LibGDX comes with a ...

  6. Rscripts的使用,以及如何为R脚本传参数

    一.在windows下使用Rscript: 1.进入cmd.exe下,将当前工作目录转到Rscript.exe所在目录下面,然后调用Rscript  **.R文件即可.但是所调用的.R文件必须是在R的 ...

  7. LR中错误代号为27796的一个解决方法

    问题:   曾经遇到过一个问题,在一次性能测试过程中,使用http协议的多用户向服务器发送请求.设置了持续时间,出现错误为:27796, Failed to connect to server 'ho ...

  8. 【3】JVM-OutOfMemory异常重现

    JVM中常见的OOM,那么如何通过自己编写代码产生这些OOM异常呢?通过写代码重现异常,是为了避免在工作中写出有OOM BUG的代码.之前虽然看过相关文章,但是没自己写过这些代码,这次在编写的实际过程 ...

  9. (笔记)Linux线程编译undefined reference to 'pthread_create'

    在使用线程时,使用gcc或arm-linux-gcc编译时,会出现错误:undefined reference to 'pthread_create' 主要是以下两种原因: 1.#include &l ...

  10. python程序的输入输出(acm的几个小程序)

    1,  A+B Problem : http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1000 #! ...