I believe that I found the answer, although in the process I realized that I really do not need to add an argument for my purpose, I just need to add a link to this function. In any case, for the purpose of the answer, which achieves what I initially thought was necessary, I came up with:
var someFunc = function(arg1,arg2){document.write(x+'<br />'+arg1+'<br />'+arg2)}; var addRequired = function(argName, argValue, fn){ var funcString = String(fn) var paramPattern = new RegExp ('^function\\s*\\([^\\)]*\\b'+argName+'\\b[^\\)]*\\)'); var alreadyExists= paramPattern.test(funcString); if(!alreadyExists) { var insertPoint1 = funcString.indexOf('(')+1; var insertPoint2 = funcString.indexOf('{')+1; var newFunc = funcString.slice(0,insertPoint1)+argName+',' +funcString.slice(insertPoint1, insertPoint2)+argName+' = '+argValue+';' +funcString.slice(insertPoint2); return eval('fn = '+newFunc); } } someFunc = addRequired('x', 20, someFunc); someFunc(1,2,3);
What tests check if arg exists, and if not, outputs:
20 2 3
Thus, this led to the addition of an argument with the required value. I do not know if this is the "best" way to achieve it, but it worked.
Edited 7/9/10 to add this information:
I found that the above had a βmistakeβ when I went to apply it to a different situation than my example above (I donβt remember what the error was, which was a few weeks ago). I came up with something that worked. NOTE. The following does not do any check to check if an argument already exists for the function passed, and in fact it is currently expecting a function that DOES NOT TAKE THE ARGUMENTS OF ITSELF (I am sure that this could be rewritten to accommodate a function with arguments similar to what I did above). Here is a simple outline of what I actually used:
var addRequired = function(thisObj, func) { var str = 'myAddedFunction = function(x, y) {return x+y}'; str = str + String(func).replace(/^function\s*\([^\)]*\)[\s\n]*{/, ' ').replace(/}$/, ''); var newFunc = new Function('myArg1', 'myArg2', str); return function() {return newFunc.apply(thisObj, arguments)}; }
The return value is assigned to the original function passed, therefore:
var userFunc = function() { *do some stuff* };
This allows me to write a function, knowing in advance that they will have access to the function that I supply ( myAddedFunction ), as well as to the parameters that I supply ( myArg1 , myArg2 ) within their function. It may seem really abstract, for example, βwhy do I need it,β but for the project I'm working on, I (and the user) need it, because I use the user function (with my additions) to perform some operations for the user based on parameters which I supply.
I forgot to mention that for my purposes I did not need to pass the string that I was going to add to the new variables (these are always the same variables that I add), but if I had, I would do something like this, what was done in the first part of my post, where I went through (possibly an array) of strings to add to the argument values, and used them to create my newFunc with.