A, functions,

1, define,

A function is also called a method, in order to achieve some function

2, grammar,

Function name (){}Copy the code

3. Function execution

function fn(){ alert(1); } fn() // function executionCopy the code

4. Perform functions

When the function is executed, it creates a new memory space, makes a copy of the string stored in the heap, and executes the code from top to bottom. When a function executes, it creates a new private execution context (execution stack) to protect its private variables from external interference. This mechanism is called closureCopy the code
function fn(){
   var res=2;
}
console.log(res); // res is not defined
Copy the code

5, return return value

The function returns as much as it returns, and undefined if there is no return

  function fn(){
      return 100
   }
   console.log(fn());  // 100
   function fn(){
     .....
   }
   console.log(fn()); // undefined
Copy the code

6. Parameter, argument

[parameter] : formal parameter, in the definition of the function, we only first reserved two entries, the real data to pass do not know.

Argument: The argument passed to the function when it is actually executed

Find the sum of two numbers

When the function executes:

  • Parameter value
  • Variable ascension
  • The code executes from the top down
function total(n,m){ return n+m; } the total (1, 3);Copy the code

8, do not know how many parameters to pass, find the sum of all the numbers

[arguments] : Class array

Function total(){console.log(arguments)} total(3,4,5,6) {var res=null; for(var i=0; i<arguments.length; i++){ res+=arguments[i]; } return res; } var res = total,4,5,6 (3); console.log(res);Copy the code

9, find the sum of all numbers (consider invalid characters)

function total(){ var res=null; for(var i=0; i<arguments.length; i++){ var item=Number(arguments[i]); if(isNaN(item)){ item=0; } res=res+item; } return res; } var result1=total("1",2,"16px",3); console.log(result1);Copy the code

[Upgrade 2]

If the argument passed contains a string, it becomes a number. If it is a non-valid number, it is skipped

function fn(){ var total=null; for(var i=0; i<arguments.length; i++){ var item=Number(arguments[i]); isNaN(item)? null:total+=item } return total; }Copy the code

[Advanced Version 3: ES6]

function fn(... arg){ return eval(arg.filter((item)=>! IsNaN (item)). The join (" + "))} var res = fn (1, 2, 3, "3", "3 p");Copy the code

10. Way of receiving parameters:

  • parameter
  • arguments

Real-name and anonymous functions

  • Real-name functions: functions that have names
  • Anonymous functions: functions that have no name, the more common ones are
  • Function expression: Assigns an anonymous function to a variable or event
    • Self-executing functions: Creation and execution are done together. ((), +, ~!
/ / = = = = = real-name function function fn () {} / / = = = = = > anonymous functions / / = = = = = = = > function expression var f1 function () {} / / = = = = = = > function expression odiv. The onclick = function () { } // function(){})(); ~function(){ }(); +function(){ }(); -function(){ }(); ! function(){ }();Copy the code

12. Arrow Function (ES6)

var fn=function(a,b){ return a+b } var fn=(a,b)=>{ return a+b; } var fn=(a,b)=> a+b; Var fn=a=>a+2; fn(3)Copy the code

Second, the judgment statement

if/else if /else
if(){
}else if(){
}else{
}
Copy the code

1. Mutual conversion rules in conditional judgment

Object == object, if the reference address is the same, equal, if the reference address is different, not equal

Objects of type == number are converted to numbers before comparison

When an object == is a string, the object is converted to a string and compared

Object == Boolean data types are first converted to numbers in comparison

Number == string, both first convert the number type, then compare

Number == Boolean, all first converted to numeric type, then compared

The string == Boolean is converted to a numeric type and then compared

//========== For different data types conversion rules: When converting an object to a string, the object is converted to a string first and then the comparison is performed. Null ==undefined true null===undefined false null and undefined are never equal to other values NaN and other values are never equal to ==========Copy the code

[2.1] Practice:

1==true ===>true 1==false ====false 2==true ===>false []==true ==>false ! []==true ==> false []==[] var ary1=[1,2]; var ary2=ary1 ary1==ary2;Copy the code