2019/8 / 14js notes

function:

Case-sensitive function strictly city; and repeating generated coverage. '

Function by function keyword definition , declare the function simply declare once, use many times.

Since calling a function:

Function expression can be used as "self-call."

Self-expression is invoked automatically be called (to start), and without the calling.

Function expressions will be automatically executed, if followed later in the expression ().

You can not call itself a function declaration.

You need to add parentheses around the function to indicate that it is a function expression;

function calc(num1,num2){
   return num1+num2;
  }
alert(calc());
alert(window.calc(3,5));
 alert(calc(1,2,3,4,5,6));

num1, num2 characteristic parameter is: effect, long lifetime only once.

The following are the arguments alert.

Implement default parameters:

function calc1(num1,num2){
   num1=num1||1;
   num2=num2||2;
   return num1+num2;
  }

Output is 3.

Judge:

function calc4(x,y){
  if(x===undefined){
   x=0;
  }
  y=y===undefined?0:y;
  return x+y;
 }

Variable parameter function of the form:

Test function () {
  var = paramsNum The arguments.length; // get the number of parameters passed
  var SUM = 0;
  for (var I = 0; I <paramsNum; I ++) {
   SUM + = arguments [I];
  }
  SUM return;
 }
Alert (Test (1,2,3,4,5,6));

Seeking maximum:

function test1(){
  var paramsNum=arguments.length;
  var max=0;
  for(var i=0;i<=paramsNum-1;i++){
   if(arguments[i]>max){
    max=arguments[i];
   }
  }
  return max;
 }

alert(test1(123,3432,23456,445643));

Global variables: Scope for the entire program.

Local variables: the scope of the current function or cycle.

Global and local variables difference:
1. different scopes: scope of global variables for the entire program, and the scope of local variables for the current function or circulation.
2. Different storage memories: the global variables stored in the global data area, local variables are stored in the stack area.
3. different life cycle: life cycle global variables and the main program, as with the destruction process and destruction, local variables within a function or internal circulation, with exit or exit loop function does not exist.
4. Use different ways: Global variables can be used in various parts of the program after the statement, but local variables can only be used locally. Internal function will prefer to use local variables and then use global variables.
 Note that local variables can not be assigned the same name as a global variable.

Guess you like

Origin www.cnblogs.com/zhang666/p/11353731.html