【问题标题】:Using this keyword with strict mode with plain javascript [duplicate]使用纯javascript的严格模式使用此关键字[重复]
【发布时间】:2021-05-23 18:57:44
【问题描述】:

我正在尝试在函数中使用“this”关键字,这很有效

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();
})();

似乎在严格模式下必须在对象内部才能使用 this 关键字,而不仅仅是在函数内部

【问题讨论】:

    标签: javascript strict


    【解决方案1】:

    如果你只是想将window对象应用到这里,你可以使用call(window)apply(window)

    申请:https://www.w3schools.com/js/js_function_apply.asp

    电话:https://www.w3schools.com/js/js_function_call.asp

    【讨论】:

    • 不是我要找的东西,但您的链接将我指向正确的位置
    猜你喜欢
    • 2011-09-12
    • 2015-06-07
    • 2013-01-31
    • 1970-01-01
    • 2015-03-02
    • 2014-08-13
    • 2018-07-19
    • 2012-11-09
    • 1970-01-01
    相关资源
    最近更新 更多