| home / programming / javascript / mk / 1 | [previous] |
|
|
Now that we have everything we need, we'll piece it together. We're going to create a "fake" function for each function that we want to auto-load. When these "fake" functions are called, they will load the real file and then re-execute the real function.
for(var fileName in autoIncludeFunctions){
var functionNames = autoIncludeFunctions[fileName];
for(var i=0; i<functionNames.length; i++){
var functionName = functionNames[i];
window[functionName] = function(){
loadScript(fileName, functionNames);
var execCode = functionName+'( ';
for(var i=0; i<arguments.length; i++) execCode+='arguments['+i+'],';
execCode = execCode.substr(0, execCode.length-1)+')';
return eval(execCode);
}
}
}
The first part is relatively simple. We are looping through our list of files, one at a time, and then looping through each function in those files. Let's break down the inner code, so that it's a bit easier to understand:
window[functionName] = function(){
loadScript(fileName, functionNames);
Here, we're using a global setting for our new "fake" function. We use the window object so that this function will be available throught the code. Inside of this function, we're calling the loadScript function that we previously created. After this is called, our fake global function is replaced with the real one, and we're halfway home. We now have to call the real function with all of the parameters passed to the fake one.
var execCode = functionName+'( '; for(var i=0; i<arguments.length; i++) execCode+='arguments['+i+'],'; execCode = execCode.substr(0, execCode.length-1)+');';
We now generate a string with which to call our real function. If you didn't already know, the arguments object contains all of the parameters passed into a function. We loop through the arguments object so that we can pass each and every parameter sent to this fake function on to the real one. At the end, this string will look something like "functionName(arguments[0], arguments[1], arguments[2]);".
return eval(execCode);
Now that we have a string containing the proper call to our real function, we just have to execute it and we're done! Our real function has now replaced the fake one so that we don't have to go through all of this again, and it's been called. Any value that the real function would return gets sent to the original calling code as it should.
If you are implementing this code, you might want to take the following into account:
If you want to see this concept in more detail, take a look at my general.js file. It's not implemented exactly as portrayed here, but it's fairly close, and is a bit more robust.
You now have the ability to access any JavaScript file you've ever created, all from one script. The examples in this article are straightforward, but it shouldn't be hard to modify this code to add validation to see if the file exists, prevent the code from overriding other functions, and any other features you may need.
Mark Kahn is a Web Developer and DBA. Mark can be contacted through his website, http://www.jslibrary.org.
| home / programming / javascript / mk / 1 | [previous] |
Created: June 5, 2003
Revised: March 1, 2006
URL: http://webreference.com/programming/javascript/mk/1