Dynamically bind argument and default value to existing function in Javascript

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-06-17T19:10:17Z Indexed on 2010/06/17 19:13 UTC
Read the original article Hit count: 314

Let's suppose you have some function someFunc() already defined in javascript, that may or may not have its own argument set defined. Is it possible to write another function to add a required argument and set that argument to a default for someFunc()? Something like:

var someFunc = function(arg1, arg2 ...){ Do stuff...}
var addRequired = function(argName, argValue, fn) { 
    Add the required default arg to a function...
}

addRequired("x", 20, someFunc);

Now someFunc would be defined roughly like so:

someFunc = function(x, arg1, arg2...) {
    x = 20;
    Do stuff...
}

What I am really seeking is to not only bind a this value to a function (which I already know how to achieve), but also bind another object reference to that same function (the function not being known ahead of time, as a user will define it, but then the user's code has access to this second object reference for use in their own function). So in my simple example above, the "20" value will actually be an object reference.

Thanks for any help you can offer.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dynamic