Is there any way to add a function to an object without knowing it's name in advance?
I do something like:
var $functionName = "sayHello";
object."$functionName" = function (args) {
// Do stuff
}
/// Later
object.sayHello ("Henry");
Is there any way to add a function to an object without knowing it's name in advance?
I do something like:
var $functionName = "sayHello";
object."$functionName" = function (args) {
// Do stuff
}
/// Later
object.sayHello ("Henry");
Yes:
var functionName = "sayHello";
anObject[functionName] = function (args) {
// ...
}
Note that a.b
is syntactic sugar for a["b"]
-- they mean exactly the same thing.
You could use the array format for this:
object[$functionName] = function () {}