我试图了解“使用严格”的“this”规则;在以下情况下进行修改。
阅读(http://unschooled.org/2012/03/understanding-javascript-this/)后,我最好的猜测是,由于函数 isStrictModeOn() 没有“附加”到任何东西,这指的是空值。这被认为是仅将 this 附加到全局对象的 Javascript 更明智的替代方案。这是对在这种情况下“使用严格”所做的更改的正确解释吗?
http://www.novogeek.com/post/ECMAScript-5-Strict-mode-support-in-browsers-What-does-this-mean.aspx
function isStrictMode(){
return !this;
}
//returns false, since 'this' refers to global object and '!this' becomes false
function isStrictModeOn(){
"use strict";
return !this;
}
//returns true, since in strict mode, the keyword 'this' does not refer to global object, unlike traditional JS. So here,'this' is null and '!this' becomes true.