Javascript-jquery-Function parameters

Source: Internet
Author: User

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

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.