JavaScript参考手册
错误 | Errors
Errors: Strict Non Simple Params
信息
Firefox:
SyntaxError: "use strict" not allowed in function with default parameter
SyntaxError: "use strict" not allowed in function with rest parameter
SyntaxError: "use strict" not allowed in function with destructuring parameter
Chrome:
SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list
错误类型
SyntaxError
.
哪里出错了?
一个"use strict"
指令是在具有下列参数之一的函数的顶部写:
- 默认参数
- 休息参数
- 解构参数
根据"use strict"
ECMAScript规范,在这些函数的顶部不允许有指令。
示例
函数声明
在这种情况下,该功能sum
具有默认参数a=1
和b=2
:
function sum(a = 1, b = 2) {
// SyntaxError: "use strict" not allowed in function with default parameter
'use strict';
return a + b;
}
如果函数应该处于严格模式,并且整个脚本或函数也可以处于严格模式,那么可以将该"use strict"
指令移动到函数之外:
'use strict';
function sum(a = 1, b = 2) {
return a + b;
}
函数表达式
函数表达式可以使用另一个解决方法:
var sum = function sum([a, b]) {
// SyntaxError: "use strict" not allowed in function with destructuring parameter
'use strict';
return a + b;
};
这可以转换为以下表达式:
var sum = (function() {
'use strict';
return function sum([a, b]) {
return a + b;
};
})();
箭头功能
如果一个箭头函数需要访问该this
变量,则可以使用箭头函数作为封闭函数:
var callback = (...args) => {
// SyntaxError: "use strict" not allowed in function with rest parameter
'use strict';
return this.run(args);
};
这可以转换为以下表达式:
var callback = (() => {
'use strict';
return (...args) => {
return this.run(args);
};
})();
错误 | Errors相关

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络