1

I was wondering how to use the $.proxy with an anonymous function

(function hours() {
   //some stuff here
})();

And then something like

($.proxy(function hours() {
//some stuff here
},this)();

But that doesnt work ? Any ideas

Andy
  • 15,873
  • 12
  • 40
  • 52

1 Answers1

1

$.proxy is just going to maintain the executing context, it was intended to close over this. A good explanation of proxy overall can be found here: Understanding $.proxy() in jquery?

The correct usage for your demo would be

var prox = $.proxy(function hours() {
 //some stuff here
},this);

And then you can use prox() later on and it will share the this scope you closed over. If this was the window, then it really didn't accomplish much. As for the anonymous function, it doesn't really matter if it is named or not.

I relocated the name above just to show that you are essentially storing a function. When prox() is used from this example, it essentially uses hours.apply(this-closed-scope).

Community
  • 1
  • 1
Travis J
  • 77,009
  • 39
  • 185
  • 250