将 THIS 传递给立即调用的函数表达式 [英] Passing THIS into an Immediately-Invoked Function Expression

查看:41
本文介绍了将 THIS 传递给立即调用的函数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 this 传递到立即调用的函数表达式中,而不解析为 var that = this(在某些情况下不适用)?

Is there any way to pass this into an Immediately-Invoked Function Expression, without resolving to var that = this (which isn't applicable on some cases)?

尝试了以下但没有运气:

Tried the following but with no luck:

(function(that) {
    console.log(that);
})(this)

推荐答案

您或许可以使用 callapply 用于此目的.例如:

You might be able to use call or apply for this purpose. For example:

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

这篇关于将 THIS 传递给立即调用的函数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆