3

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mrchief
  • 75,126
  • 20
  • 142
  • 189

1 Answers1

3

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").

Pointy
  • 405,095
  • 59
  • 585
  • 614