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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
---
title: 'SyntaxError: "use strict" not allowed in function with non-simple parameters'
slug: Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params
tags:
- Errors
- JavaScript
- SyntaxError
- TypeError
- use strict
translation_of: Web/JavaScript/Reference/Errors/Strict_Non_Simple_Params
---
<div>{{jsSidebar("Errors")}}</div>
<h2 id="信息">信息</h2>
<pre class="syntaxbox">Firefox:
句法错误: "use strict" 不允许在带默认参数的函数中
句法错误: "use strict" 不允许在带rest参数的函数中
句法错误: "use strict" 不允许在带解构参数的函数中
Chrome:
句法错误: 非法的'use strict'指令,在带有非简单参数列表的函数中
</pre>
<h2 id="错误类型">错误类型</h2>
<p>{{jsxref("SyntaxError")}}.</p>
<h2 id="哪里出错了">哪里出错了?</h2>
<p>在函数顶部直接写了 <code>"use strict"</code> ,而该函数拥有以下的参数其中之一:</p>
<ul>
<li>{{jsxref("Functions/Default_parameters", "默认参数", "", 1)}}</li>
<li>{{jsxref("Functions/rest_parameters", "剩余参数", "", 1)}}</li>
<li>{{jsxref("Operators/Destructuring_assignment", "解构赋值", "", 1)}}</li>
</ul>
<p>根据ECMAScript规范,不允许在这些函数的顶部使用“use strict”指令。</p>
<h2 id="示例">示例</h2>
<h3 id="函数语句">函数语句</h3>
<p>在这种情况下,函数sum具有默认参数a = 1和b = 2:</p>
<pre class="brush: js example-bad">function sum(a=1, b=2) {
// SyntaxError: "use strict" not allowed in function with default parameter
"use strict";
return a + b;
}
</pre>
<p>如果这个函数应该处于 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a>,并且整个脚本或封装函数也可以在严格模式下,可以移动 "use strict" 指令到函数之外:</p>
<pre class="brush: js example-good">"use strict";
function sum(a=1, b=2) {
return a + b;
}
</pre>
<h3 id="函数表达式">函数表达式</h3>
<p>函数表达式可以使用另一种解决方法:</p>
<pre class="brush: js example-bad">var sum = function sum([a, b]) {
// SyntaxError: "use strict" not allowed in function with destructuring parameter
"use strict";
return a + b;
};
</pre>
<p>这可以转换为以下表达式:</p>
<pre class="brush: js example-good">var sum = (function() {
"use strict";
return function sum([a, b]) {
return a + b;
};
})();
</pre>
<h3 id="箭头函数">箭头函数</h3>
<p>如果箭头函数需要访问 <code>this</code>,则可以将箭头函数作为封闭函数来使用:</p>
<pre class="brush: js example-bad">var callback = (...args) => {
// SyntaxError: "use strict" not allowed in function with rest parameter
"use strict";
return this.run(args);
};
</pre>
<p>这可以转换为以下表达式:</p>
<pre class="brush: js example-good">var callback = (() => {
"use strict";
return (...args) => {
return this.run(args);
};
})();
</pre>
<h2 id="也可以看看">也可以看看</h2>
<ul>
<li>{{jsxref("Strict_mode", "Strict mode", "", 1)}}</li>
<li>{{jsxref("Statements/function", "函数语句", "", 1)}}</li>
<li>{{jsxref("Operators/function", "函数表达式", "", 1)}}</li>
<li>{{jsxref("Functions/Default_parameters", "默认参数", "", 1)}}</li>
<li>{{jsxref("Functions/rest_parameters", "剩余参数", "", 1)}}</li>
<li>{{jsxref("Operators/Destructuring_assignment", "解构参数", "", 1)}}</li>
</ul>
|