xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!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

posted @   xgqfrms  阅读(200)  评论(6编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2015-10-07 css3 online generate tools
2015-10-07 html5 image>usemap (attribute)
点击右上角即可分享
微信分享提示