Equivalent of ASP.Net Ajax Function.createDelegate in jQuery

孤者浪人 提交于 2019-12-13 13:44:28

问题


I was trying to port over a ASP.Net ajax behavior into a jQuery plugin. A piece of puzzle that remains solving is to find a substitute for Function.createDelegate in jQuery.

I need something like this in jQuery:

this.$delegateOnClick = Function.createDelegate(this, this.fireOnClick);

Is jQuery .delegate method the way to go?

Or is it this post: Controlling the value of 'this' in a jQuery event


回答1:


I think you want the jQuery $.proxy() function. There are two forms:

var proxy = $.proxy(someFunction, someObject); // A
var proxy2 = $.proxy(someObject, someString); // B

The "A" invocation returns a function such that when it's called, the function "someFunction" will be called with whatever arguments you passed the proxy and with "this" bound to "someObject". The "B" version is similar, but instead of you passing in a function you pass in the name of a property on "someObject" that refers to some function. Thus, if you have an object "widget" with a function called "blink", then

var blinker = $.proxy(widget, "blink");

gives you a function that will invoke the "blink" function on "widget" (that is, with "this" bound to "widget").



来源:https://stackoverflow.com/questions/4409920/equivalent-of-asp-net-ajax-function-createdelegate-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!