function parameters (Parameter Mapping)
Multi-parameter
When a function has more than one parameter, if we pass a large number of arguments when calling the function, and some parameters are not necessary, the code looks a bit confusing and can be solved by using an anonymous object. Use a parameter as a property of an anonymous object, and then use the anonymous object as a parameter to the function.
Parameter substitution
Inside a function, you can define an object that sets the property of the object to the default value of the function parameter.
AA (Options) { // Configure default parameters var param = {gender: ' man ', Age:18}; // extend method was originally used to extend the method for jquery, But when it takes two arguments, it does so by merging the parameters and default parameters passed by the user instead of the extension method. The parameters used by the merged function are passed by the user, and the parameters that are not passed are automatically replaced with default values. $.extend (param, options); alert (param.age); }aa (); // call AA, do not supply parameters will use the default parameter AA ("woman"); // provides one argument, and the other uses the default parameter AA ("Woman", 20); // provides two parameters and no longer uses the default parameter
View Code
Depth substitution
If a property of a function's argument (object) is also an object, all merges need to provide a Boolean value for extend.
functionAA (options) {varOP = $.extend (true, {gender: "Man", Job: {Salary: ' + ', Typeswork: ' Scientist ', age:30}}, Options); returnop;}varTest=aa ({name: ' Leo ', job: {Salary: ' 100000 ', typeswork: ' Programmer ') } }); for(varIinchtest) { varJobobj =Test[i]; if(typeofJobobj = = "Object") { for(varZinchjobobj) {Alert (z+ ":" +Jobobj[z]); } } Else{alert (i+ ":" +Test[i]); }}//The result of the parameter substitution is: {name: ' Leo ' {gender: ' Man ', job: {Salary: ' 100000 ', typeswork: ' Programmer ', age:30}}//If you specify false for the first argument, the result ignores the age
View Code
Javascript-Learning Total Directory
Javascript-jquery-Function parameters