How To Create A Variadic (with Variable Length Argument List) Function Wrapper In Javascript
The intention is to build a wrapper to provide a consistent method of calling native functions with variable arity on various script hosts - so that the script could be executed in
Solution 1:
Just use apply()
. And for your antiquated execution engines, just do this
if ( 'undefined' == typeofFunction.prototype.apply )
{
Function.prototype.apply = function( context, args )
{
// whatever hacky way you want to implement it - i guess eval.
}
}
Solution 2:
Just like the automatic 'this' variable, there is an 'arguments' variable that holds all the arguments passed in to the function. See javascript variadic.
Baca Juga
- How Can I Increase A Number By Activating A Button To Be Able To Access A Function Each Time The Button Is Increased?
- What Is Missing From This Description For Nested Functions And Closures At Mozilla Developer Network?
- In Javascript V8 Does Compilation Phase Happen To Functions Before Execution Phase Then All The Code Is Executed Or Only For Global Context
Post a Comment for "How To Create A Variadic (with Variable Length Argument List) Function Wrapper In Javascript"