1

1

1

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

"use strict";定义JavaScript代码应该在"strict mode"下被执行。

"use strict"指令是JavaScript 1.8.5 (ECMAScript的第5版)新增的。

这不是一个声明,而是一个字面量表达,会被早期版本的JavaScript忽略掉。

"use strict"的目的是为了指明代码应该在"strict mode"下被执行。

1

1

http://www.w3schools.com/js/js_strict.asp

# Javascript 严格模式("use strict";)详解

## [http://www.w3schools.com/js/js_strict.asp](http://www.w3schools.com/js/js_strict.asp)

```code
"use strict";定义JavaScript代码应该在"strict mode"下被执行。

"use strict"指令是JavaScript 1.8.5 (ECMAScript的第5版)新增的。

这不是一个声明,而是一个字面量表达,会被早期版本的JavaScript忽略掉。

"use strict"的目的是为了指明代码应该在"strict mode"下被执行。

例如,在"strict mode"下,你不能使用未被声明的变量。
``` 
### 声明严格模式
严格模式的声明是通过将"use strict";添加到一个脚本或一个函数的开头位置来实现的。

```js
"use strict";
x = 3.14;       
// 这将会产生一个错误,因为x是未被声明的。

"use strict";
myFunction();

function myFunction() {
    y = 3.14;
    // 这将会产生一个错误,因为y是未被声明的。
}
``` 

### 在一个函数内部中声明 "use strict";,它具有局部作用域(只在函数内部的代码是执行在严格模式下)

```js
x = 3.14;       
// 这将会产生一个错误,因为x是未被声明的。
myFunction();

function myFunction() {
   "use strict";
    y = 3.14;   
    // 这将会产生一个错误,因为y是未被声明的。
}
``` 

## 为什么要使用严格模式?

声明严格模式的语法,被设计是为了兼容旧版本的JavaScript。

严格模式可以更容易地编写“安全”的JavaScript。

严格模式将以前被接受的“错误语法”改变为真正的错误。

例如,
在标准的JavaScript中,错误输入一个变量名会创建一个新的全局变量。
在严格模式下,这将抛出一个错误,这使得意外地创建一个全局变量成为不可能。

在标准的JavaScript,开发人员将不会收到任何错误反馈,赋值给非可写的属性。
在严格模式下,赋值给任何:一个非可写的属性,一个只读的属性,一个不存在的属性,
一个不存在的变量,或一个不存在的对象,都将抛出一个错误。

面向未来! 
未来保留的关键字在严格模式是不允许的。
implements
interface
let
package
private
protected
public
static
yield

# 小心!

"use strict"指令只能在一个脚本或一个函数的开头被识别。

1

1

demo:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Javascript 严格模式.html</title>
</head>
<body>
<div>
<h1>Javascript 严格模式.html</h1>
</div>
<pre>
<!-- -->
</pre>
<script>
/*
"use strict";
x = 3.14;
// x is not defined "use strict";
myFunction();
function myFunction() {
y = 3.14;
// 这将会产生一个错误,因为y是未被声明的。
}; myFunction();
function myFunction() {
"use strict";
y = 3.14;
// y is not defined
}
*/
/*
"use strict";
var x = 3.14;
delete x;
// Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
"use strict";
function x(p1, p2) {};
delete x;
// Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.(不合格的标识符不允许)
*/
/*
"use strict";
var x = 010;
// Uncaught SyntaxError: Octal literals are not allowed in strict mode. (八进制字面量不允许)
"use strict";
var x = \010;
// Uncaught SyntaxError: Invalid or unexpected token(转义字符不允许)
*/
/*
"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14;
//写入只读属性是不允许的:
"use strict";
var obj = {get x() {return 0} };
obj.x = 3.14;
//写入get-only(只能获得)属性是不允许的:
"use strict";
delete Object.prototype;
//删除一个不可删除的属性是不允许的:
"use strict";
var eval = 3.14;
//字符串“eval”不能用作变量:
"use strict";
var arguments = 3.14;
// 字符串“arguments”不能用作变量:
"use strict";
with (Math){x = cos(2)};
// with语句是不允许的
"use strict";
eval ("var x = 2");
alert (x);
// 出于安全的原因,在eval()被调用的作用域内,是不允许它创建变量的!
"use strict";
var public = 1500;
// 未来保留的关键字在严格模式是不允许的。
*/
</script>
</body>
</html>

1

1

1

1

1

1

1

1

1

1

1

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

"use strict";定义JavaScript代码应该在"strict mode"下被执行。

"use strict"指令是JavaScript 1.8.5 (ECMAScript的第5版)新增的。

这不是一个声明,而是一个字面量表达,会被早期版本的JavaScript忽略掉。

"use strict"的目的是为了指明代码应该在"strict mode"下被执行。

1

1

http://www.w3schools.com/js/js_strict.asp

# Javascript 严格模式("use strict";)详解

## [http://www.w3schools.com/js/js_strict.asp](http://www.w3schools.com/js/js_strict.asp)

```code
"use strict";定义JavaScript代码应该在"strict mode"下被执行。

"use strict"指令是JavaScript 1.8.5 (ECMAScript的第5版)新增的。

这不是一个声明,而是一个字面量表达,会被早期版本的JavaScript忽略掉。

"use strict"的目的是为了指明代码应该在"strict mode"下被执行。

例如,在"strict mode"下,你不能使用未被声明的变量。
``` 
### 声明严格模式
严格模式的声明是通过将"use strict";添加到一个脚本或一个函数的开头位置来实现的。

```js
"use strict";
x = 3.14;       
// 这将会产生一个错误,因为x是未被声明的。

"use strict";
myFunction();

function myFunction() {
    y = 3.14;
    // 这将会产生一个错误,因为y是未被声明的。
}
``` 

### 在一个函数内部中声明 "use strict";,它具有局部作用域(只在函数内部的代码是执行在严格模式下)

```js
x = 3.14;       
// 这将会产生一个错误,因为x是未被声明的。
myFunction();

function myFunction() {
   "use strict";
    y = 3.14;   
    // 这将会产生一个错误,因为y是未被声明的。
}
``` 

## 为什么要使用严格模式?

声明严格模式的语法,被设计是为了兼容旧版本的JavaScript。

严格模式可以更容易地编写“安全”的JavaScript。

严格模式将以前被接受的“错误语法”改变为真正的错误。

例如,
在标准的JavaScript中,错误输入一个变量名会创建一个新的全局变量。
在严格模式下,这将抛出一个错误,这使得意外地创建一个全局变量成为不可能。

在标准的JavaScript,开发人员将不会收到任何错误反馈,赋值给非可写的属性。
在严格模式下,赋值给任何:一个非可写的属性,一个只读的属性,一个不存在的属性,
一个不存在的变量,或一个不存在的对象,都将抛出一个错误。

面向未来! 
未来保留的关键字在严格模式是不允许的。
implements
interface
let
package
private
protected
public
static
yield

# 小心!

"use strict"指令只能在一个脚本或一个函数的开头被识别。

1

1

demo:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Javascript 严格模式.html</title>
</head>
<body>
<div>
<h1>Javascript 严格模式.html</h1>
</div>
<pre>
<!-- -->
</pre>
<script>
/*
"use strict";
x = 3.14;
// x is not defined "use strict";
myFunction();
function myFunction() {
y = 3.14;
// 这将会产生一个错误,因为y是未被声明的。
}; myFunction();
function myFunction() {
"use strict";
y = 3.14;
// y is not defined
}
*/
/*
"use strict";
var x = 3.14;
delete x;
// Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
"use strict";
function x(p1, p2) {};
delete x;
// Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.(不合格的标识符不允许)
*/
/*
"use strict";
var x = 010;
// Uncaught SyntaxError: Octal literals are not allowed in strict mode. (八进制字面量不允许)
"use strict";
var x = \010;
// Uncaught SyntaxError: Invalid or unexpected token(转义字符不允许)
*/
/*
"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});
obj.x = 3.14;
//写入只读属性是不允许的:
"use strict";
var obj = {get x() {return 0} };
obj.x = 3.14;
//写入get-only(只能获得)属性是不允许的:
"use strict";
delete Object.prototype;
//删除一个不可删除的属性是不允许的:
"use strict";
var eval = 3.14;
//字符串“eval”不能用作变量:
"use strict";
var arguments = 3.14;
// 字符串“arguments”不能用作变量:
"use strict";
with (Math){x = cos(2)};
// with语句是不允许的
"use strict";
eval ("var x = 2");
alert (x);
// 出于安全的原因,在eval()被调用的作用域内,是不允许它创建变量的!
"use strict";
var public = 1500;
// 未来保留的关键字在严格模式是不允许的。
*/
</script>
</body>
</html>

1

1

1

1

1

1

1

1

最新文章

  1. JS中关于字符串的几个常用又容易忘记的方法
  2. 从0到1---“保多多”APP的开发(一)
  3. border 变形计
  4. [SQL]SQL语言入门级教材_SQL语法参考手册(三)
  5. HTML5入门1---Canvas画布
  6. JAVA跑马灯实现1
  7. 【转】zookeeper 的监控工具
  8. CSDN书籍下载
  9. 一次由SELinux引起的ssh公钥认证失败问题
  10. C# 面向对象基础&amp;封装&amp;继承&amp;多态&amp;加深一下冒泡排序写法
  11. ldap数据库--ODSEE--suffix
  12. 云计算--网络原理与应用--20171123--网络地址转换NAT
  13. 重装助手教你如何在Windows 10中更改您的帐户名称
  14. position三种属性的区别
  15. POJ3040--Allowance(贪心)
  16. H5 id选择器和class选择器
  17. rabbitmq基础学习+springboot结合rabbitmq实现回调确认confirm
  18. python网络编程初级
  19. python XML文件解析:用xml.dom.minidom来解析xml文件
  20. hadoop本地集群搭建

热门文章

  1. Python虚拟环境配置应用
  2. jQuery 勾选启用输入框
  3. The router relies on a tree structure which makes heavy use of common prefixes, it is basically a compact prefix tree (or just Radix tree).
  4. LDAP学习
  5. CSS居中的常用方式以及优缺点
  6. http状态码、错误分析
  7. 将一个 JavaBean 对象转化为一个 Map
  8. 1.DHCP的定义
  9. PHP-表单提交(form)
  10. 小白搭建WNMP详细教程---MYSQL安装与设置