Bind parameters to Functions
Note: The function is used as a parameter to pass, and the closure is used to bind a parameter to the function to be passed.
Application Scenario: When binding an event to a DOM element, you cannot specify parameters for the function. This function can be used to resolve this issue...
// Bind parameters to the Function
Function bindarguments (/* function */f/*, initial argemtnts ...*/){
VaR bindargs = arguments;
Return function (){
VaR ARGs = [];
For (VAR I = 1; I <bindargs. length; I ++) args. Push (bindargs [I]);
Return F. Apply (this, argS );
}
}
// Call example. Click the hyperlink on the page to bring up the corresponding index.
VaR linkarray = Document. getelementsbytagname ('A ');
For (VAR I = 0, M = linkarray. length; I <m; I ++ ){
Linkarray [I]. onclick = bindarguments (function () {alert (arguments [0]) ;}, I + 1, linkarray [I]);
}