jquery - 在jquery中设置this的值

标签 jquery this

在Mootools中,可以控制函数内this变量的值:

function foo() {
  // do something with the this variable
}

var bar = foo.bind(some_object);

// Now bar does the same thing as foo, except
// the this variable is a reference to some_object

这可以在 Jquery 中完成吗?

最佳答案

jQuery 提供了.proxy() 方法。它基本上与 .apply().call() 相同,其语法如下:

$.proxy(method, scope);

在您的示例中,它将是:

var bar = $.proxy(foo, some_object);

引用号:.proxy()

关于jquery - 在jquery中设置this的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4364842/

相关文章:

javascript - 如何在ul中选择下一个匹配的子项并触发点击事件

javascript - jquery 使用 $(this) 不起作用

javascript - 将函数作为事件处理程序的回调传递时,在我的 javascript 项目中获取不正确的值

c++ - "this"是类方法中的默认参数吗?

javascript - 防止默认,直到连续单击两次

jquery - 在动画期间接收当前宽度

jquery - 内容可编辑光标位置

jquery - 在 WordPress 中添加指向特定页面的链接

javascript - 如何将 "this"以及 v-model 从输入传递到处理程序

javascript - 在javascript中动态生成id引用的问题