在纯javascript的严格模式下使用此关键字
我试图在函数中使用“this”关键字,这是可行的在纯javascript的严格模式下使用此关键字,javascript,strict,Javascript,Strict,我试图在函数中使用“this”关键字,这是可行的 var test = function(){ console.log(this); //console logs the window object } (function(){ test(); }); 但是,当我尝试使用严格模式时,这是未定义的 'use strict'; var test = function(){ console.log(this); //undefined } (function(){ t
var test = function(){
console.log(this); //console logs the window object
}
(function(){
test();
});
但是,当我尝试使用严格模式时,这是未定义的
'use strict';
var test = function(){
console.log(this); //undefined
}
(function(){
test();
});
我想知道是否有可能在严格模式下从内部函数访问this关键字
非常感谢您的帮助
所以对于任何面临同样问题的人来说
这很有效
'use strict';
var test = {
fname:'kalesh',
check: function(){
console.log(this.fname);
}
};
(function(){
test.check();
})();
在严格模式下,似乎必须在对象内部才能使用此关键字,而不仅仅是在函数内部如果您只想将窗口对象应用于此,可以使用
调用(窗口)
或应用(窗口)
适用于:
呼叫:不是我要找的,但你的链接将我指向了正确的asnwer