JS default parameters, variable parameters, variable scope

Source: Internet
Author: User
Tags variable scope

Variable parameters can be implemented by arguments objects

In the function code, with special object arguments, the developer can access them without having to explicitly specify the parameter names.

Arguments is an array object that can be used to index the position of the actual parameter by using the. Length to obtain the number of arguments.

code example: We use arguments to change the function's default parameters

1<script>2         functiondemo (x, y) {3x = arguments[0]?arguments[0]:1;//Arguments[0] Represents the first parameter of the demo function4y = arguments[1]?arguments[1]:2;//Arguments[1] Represents the second parameter of the demo function5             returnx+y6         }7Alert (Demo (4,5))//we call the parameters of the function =>98Alert (Demo ())//we call the default parameter =>39</script>

code example: the parameters implemented by Arguments.length to get the length of the parameter and

1<script>2          functionDemo () {3              varCount = Arguments.length//Save the length of the argument in the variable of Count4              varSum =0//A variable that is a sum of Fame5               for(varI =0;i<count;i++) {//use a For loop to traverse the length of all parameters6Sum+=arguments[i]//calculate the value of the corresponding subscript parameter for each arguments7              }8              returnSum//return the value to the function9          }TenAlert (Demo (1,2,3,4,5,6))//= = Pass in the parameters you have defined, =>21 One</script>

code example: the maximum number of parameters implemented by Arguments.length to get the length of the parameter

1<script>2          functionDemo () {3              varCount = Arguments.length//Save the length of the argument in the variable of Count4              varMax =0//fame a maximum value5               for(varI =0;i<count;i++) {//use a For loop to traverse the length of all parameters6                  if(Arguments[i]>max) {//make an If statement of judgment, traverse out all the maximum values in turn to max, direct convenience end7Max =Arguments[i]8                  }           9              }Ten              returnMax//return the value to the function One          } AAlert (Demo (10,20,1,4,11))//= = Pass in the parameters you have defined, =>20 -</script>

Scope of the variable:

The difference between a global variable and a local variable:

The global reputation in the function body, starting from the foot can be used to end the script.

Local variables in the function body can only be used in the function body.

code example:

1<script>2         varx = "global variable x";3         functionDemo () {4             varx = "local variable x";5 alert (x)6         }7Demo ()//= = First pops up the board variable x because we call the function to execute the code block because X is known in the function body does not conflict with global variables8Alert (x)//+ = Again is the popup global variable x, because we are in the function outside of the alert (x), can only call the variables of the global function, when the function is completed, automatically reclaims the contents of the function body9</script>

It is best to use the VAR statement to declare a variable:

code example:

1<script>2        varX =13        functionDemo () {4X =2//if the function body variable does not have the var keyword, the variable automatically becomes the global variable5 alert (x)6        }7Demo ()//= = 2 Output result8Alert (x)//= = At this point the output is also 2, because variables in the function body are global variables, which are overwritten when the names of the variables are assigned9</script>

Comprehensive parsing of global and local variables with a mixed code-listing

1<script>2     //parse global variables and local variables3       varx = 1,y = 2;//This is the two global variables x and y that are declared outside of the function body within the script scripts4       functiondemo (x, y) {5document.write (x)//=>undefined because no parameters are written when the function is called, the parameter with no value defaults to undefined6document.write (y)//Ibid .7           varx = 5,y = 6;//two local variables x and Y are declared here in the function body8document.write (x)//since the JS code is executed from top to bottom, the X in this case is a local variable x9document.write (y)//Ibid .TenZ =x+y//a z is declared in the function body without Var, this is a global variable, but it is assigned in the function body, x, y are local variables Onedocument.write (z)//+ = output result is one A       } - Demo () -Alert (z)//because Z is a global variable, it is possible to take the value of z outside of the function =>11 theAlert (x)//=>1 x is obtained outside the function body, and the resulting global variable x -Alert (y)//Ibid . -</script>

JS default parameters, variable parameters, variable scope

Related Article

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.