据我了解,当strict mode在 ECMAScript 5(又名 ES5)中初始化使用引用时,它是引用设置为 true 的属性。设置时strict mode,更多的操作会导致错误(语法,引用,例如初始化一个没有var关键字的变量)。有关更多信息,请参阅MDN 文档strict mode。
[编辑]基于评论,我认为它适用于strict mode定义的范围。所以在
function strict()
{ 'use strict';
// from here on and within the function
// IsStrictReference is true
showme = "Am I defined?";
return "Hi! I'm a strict mode function! " + showme;
}
function nonstrict()
{
// IsStrictReference is ... well, undefined I suppose, or false by default
showme2 = "Am I defined?";
return "Hi! I'm NOT a strict mode function! " + showme2;
}
strict(); //=> ReferenceError: showme is not defined
notstrict(); //=> "Hi! I'm NOT a strict mode function! Am I defined?"
执行 strict()抛出一个ReferenceError,但nonstrict()没有。如果您将 - 语句放置use strict在功能块之外,则执行这两个功能都会抛出ReferenceError.