I’d like something equivalent to this code, except I don’t want my_func to be called when my_var is defined. I want to call my_var() later on in the code with the original argument (‘called!’) preserved.
function my_func(first_arg) {
alert(first_arg);
};
var my_var = my_func('called!');
How?
You might be looking for something like
Function.prototype.bind, which allows you to bind a function with arguments to a particular context. Basically, it allows you to do this:It’s not in older browsers, but the link above has a compatibility implementation to make it work for this particular scenario.