"use strict" 继承/范围
"use strict" inheritance / scope
//Global Scope
"use strict"; //1
function A() {
"use strict"; //2
function innerA() {
"use strict"; //3
}
}
我只是想知道:
Is doing use strict
at //1
is enough or do we have to be explicit at all places like //2
and //3
.
在 strict mode、
上引用 MDN
To invoke strict mode for an entire script, put the exact statement "use strict";
(or 'use strict';
) before any other statements.
concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable strict mode on a function-by-function basis.
因此将它放在顶部适用于整个文件。您不必在每个函数中明确提及这一点。
注意: 在顶部使用 use strict
有其自身的问题。在链接的 MDN 页面中阅读它们。因此,根据 MDN,推荐的方法是
You can also take the approach of wrapping the entire contents of a script in a function and having that outer function use strict mode. This eliminates the concatenation problem but it means that you have to explicitly export any global variables out of the function scope.
你可以这样测试
'use strict';
(function () {
return {
1: 1,
1: 2
};
})();
现在,它会抛出一个错误,
SyntaxError: Duplicate data property in object literal not allowed in strict mode
但是,当你做这样的事情时
(function () {
return {
1: 1,
1: 2
};
})();
(function () {
'use strict';
return {
1: 1,
1: 2
};
})();
它只会在第二个函数中失败,而不是在第一个函数中。因为,只有第二个函数处于严格模式。
此外,如果您在函数中有一个函数,就像您在问题中所展示的那样,
(function () {
'use strict';
(function () {
return {
1: 1,
1: 2
};
})();
})();
由于封闭函数中的 use strict
,内部函数也将处于严格模式。因此,内部函数将引发 SyntaxError
.
但是,如果你在{}
中的一个块中使用use strict
,它不会有任何效果,例如,
(function () {
{
'use strict';
return {
1: 1,
1: 2
};
}
})();
或
console.log("");
'use strict';
var a = {
1: 1,
1: 2
};
不会抛出任何错误。
所以,use strict
应该在函数的开头,或者文件的开头。只有这样代码才会处于严格模式。
定义在//1
就够了。这直接来自 JavaScript:权威指南(重点由我添加):
The top-level (nonfunction) code of a script is strict code if the script has a "use strict" directive. A function body is strict code if it is defined within strict code or if it has a "use strict" directive.
//Global Scope
"use strict"; //1
function A() {
"use strict"; //2
function innerA() {
"use strict"; //3
}
}
我只是想知道:
Is doing
use strict
at//1
is enough or do we have to be explicit at all places like//2
and//3
.
在 strict mode、
上引用 MDNTo invoke strict mode for an entire script, put the exact statement
"use strict";
(or'use strict';
) before any other statements.concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable strict mode on a function-by-function basis.
因此将它放在顶部适用于整个文件。您不必在每个函数中明确提及这一点。
注意: 在顶部使用 use strict
有其自身的问题。在链接的 MDN 页面中阅读它们。因此,根据 MDN,推荐的方法是
You can also take the approach of wrapping the entire contents of a script in a function and having that outer function use strict mode. This eliminates the concatenation problem but it means that you have to explicitly export any global variables out of the function scope.
你可以这样测试
'use strict';
(function () {
return {
1: 1,
1: 2
};
})();
现在,它会抛出一个错误,
SyntaxError: Duplicate data property in object literal not allowed in strict mode
但是,当你做这样的事情时
(function () {
return {
1: 1,
1: 2
};
})();
(function () {
'use strict';
return {
1: 1,
1: 2
};
})();
它只会在第二个函数中失败,而不是在第一个函数中。因为,只有第二个函数处于严格模式。
此外,如果您在函数中有一个函数,就像您在问题中所展示的那样,
(function () {
'use strict';
(function () {
return {
1: 1,
1: 2
};
})();
})();
由于封闭函数中的 use strict
,内部函数也将处于严格模式。因此,内部函数将引发 SyntaxError
.
但是,如果你在{}
中的一个块中使用use strict
,它不会有任何效果,例如,
(function () {
{
'use strict';
return {
1: 1,
1: 2
};
}
})();
或
console.log("");
'use strict';
var a = {
1: 1,
1: 2
};
不会抛出任何错误。
所以,use strict
应该在函数的开头,或者文件的开头。只有这样代码才会处于严格模式。
定义在//1
就够了。这直接来自 JavaScript:权威指南(重点由我添加):
The top-level (nonfunction) code of a script is strict code if the script has a "use strict" directive. A function body is strict code if it is defined within strict code or if it has a "use strict" directive.