将其传递给立即调用的函数表达式

gsk*_*lee 1 javascript this

是否有任何方法可以传递this到立即调用的函数表达式,而无需解析var that = this(在某些情况下不适用)?

试过以下但没有运气:

(function(that) {
    console.log(that);
})(this)
Run Code Online (Sandbox Code Playgroud)

Sal*_*n A 5

您可以使用call或apply用于此目的.例如:

(function() {
    console.log(this); // whatever that was specified in the "call" method
}).call(this);
Run Code Online (Sandbox Code Playgroud)