1

I would like to pass a callback function (that contains parameters) to an object and ensure that it is invoked using specific argument values. I'd like to do this in such a way that these values do not need to be passed in as well. Is this possible?

Here is an (unusable) example of what I'm trying to do:

this.object = new Object(callback(val)); //val is the value I'd like the object to use

The callback could be one of a variety of functions with unique parameters. As such, I'd like to refrain from having to pass in the values separately.

An example:

//Instances of the same object with different callbacks
this.object1 = new Object(callback(val1, val2));
this.object2 = new Object(callback(val));
this.object3 = new Object(callback(val1, val2, val3));

As you can see, it is not feasible to pass in the additional arguments.

I could pass the callback in like this:

this.object = new Object( function() { this.callback.call(val) } );

...but since the callback is outside the scope of Object, it requires me to either pass a reference to the caller to access it, or access the caller in some other manner.

What is my best course of action?


Update:

I am creating a MenuSystem with Buttons. Each Button functions differently according to given callback methods - the primary function being for click events. An object exists in MenuSystem that encapsulates all the different functionality (methods) a button can possibly use. These methods are what's passed in as callbacks to the buttons. The Button may need to "own" the method if it modifies its own attributes. This will require a function.call()

TL;DR:

MenuSystem has [ UIMethods + UIElement ]

UIElement uses [ UIMethods.someMethod() ] for functionality purposes

someMethod() may modify attributes of UIElement which requires function.call()


sookie
  • 2,437
  • 3
  • 29
  • 48

1 Answers1

1
this.object = new Object(function() { this.callback(val) }.bind(this));

That will bind the scope so this refers correctly.

James Monger
  • 10,181
  • 7
  • 62
  • 98
  • No need for an intermediate function: `new Object(this.callback.bind(this, val));`. – Felix Kling Mar 15 '16 at 21:06
  • @JamesMonger @FelixKling: Will this successfully work if I need to use a function.call(), such that: `this.object = new Object(function() { this.callback.call(this, val) }.bind(this));` I'm assuming this will cause issues – sookie Mar 15 '16 at 21:09
  • @sookie: Why do you want to use `call` here? `this.callback()` and `this.callback.call(this)` are equivalent. – Felix Kling Mar 15 '16 at 21:09
  • @FelixKling: I need to invoke the callback as the `Object` but also get access to it since it is a function of `caller` – sookie Mar 15 '16 at 21:12
  • @sookie: I think you need to provide a better and complete(!) example. Since we don't even know what `Object` does (I assume it's not the default `Object` constructor function), we don't even know how the callback is going to be executed. Please read [mcve]. – Felix Kling Mar 15 '16 at 21:15
  • @FelixKling: Your right. I didn't think the solution would clash with the fact that I need to use `function.call()` so I didn't add it in. Editing now. – sookie Mar 15 '16 at 21:22
  • @FelixKling: Question updated – sookie Mar 15 '16 at 21:49