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()