How to dynamically add parameters in Javascript before execution

Source: Internet
Author: User

Function myFunction (test1, test2 ){
// Parameters passed in with apply must be explicitly declared and retrieved in the put order
// Alert (extendStr );
Alert (test1 );
Alert (test2 );
// After binding the property, you can call the function without passing in it, but you cannot use this
Alert ("myFunction. extendStr =" + myFunction. extendStr );
}

Function extendFunction (callbackFunction, extend ){
Var extendStr = "this is extend string! ";
Var args = [];
If (typeof (extend) = "object "){
For (var property in extend ){
// Bind property, which can be called by the function itself
CallbackFunction [property] = extend [property];
// Put the parameter values in the array in order and pass the values through apply
Args. push (extend [property]);
}
}
// Bind property, which can be called by the function itself
CallbackFunction ["extendStr"] = extendStr;
// Put the parameter values in the array in order and pass the values through apply
Args. push (extendStr );
// Call the function dynamically and input the parameter value Array
Alert ('C ');
CallbackFunction. apply (this, args );
}
// Dynamically extend and execute the function, which is generally used for processing ajax callback functions.
ExtendFunction (myFunction, {test1: 'aaa', test2: 'bbb '});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.