1, function,

Function: a block of code that is event-driven or reusable when it is called

document.onclick = function(){
}
function sum(){}
sum()
Copy the code

2. Function declaration and application scope

Common declaration:

1. Declare functions

Function name (){code block}Copy the code

Function name ();

Expression declaration:

Var var = function(){} var name ();Copy the code
// declare function sum(){console.log(" I use a function "); } // call sum(); Var f = function(){console.log(" console.log "); } f();Copy the code

Usage Scenarios:

As an event handler: document.onclick = function(){}Copy the code

Encapsulation: having a specific function

Code reuse: Duplicate code is stored in functions that need to be called

3. Function parameters

Function arguments: Arguments can be used when an uncertain value is present in a function

Parameter: formal parameter function Function name (a) {} a: parameter

Argument: Actual parameter Function name (10) 10: argument

function s(a){//var a; a = 10
  console.log(a);
}
s(10);
s(20);
Copy the code

1. Parameter number

Function The name of the function (a)

//var a,b

Parameter number can’t find it out, don’t write parameter, use the arguments, is an object, function built-in said argument set

function f(){ console.log(arguments); //Arguments(3) [10, 20, 30] console.log(arguments.length); The console / / length. The log (the arguments [1]). } f (10, 30);Copy the code

2. Parameter types

Function of the parameter type: all js data types can be finished function parameters (number, string, boolan, null, undefined, object, array, function)

Usually null and undefined are not used as function arguments, they are not meaningful, and sometimes they will report errors

Selection sort

for(var i = 0; i<array.length; i++){ //i = 0; arr[i] = 4 for(var j = i+1; j<array.length; J++) {/ / compare the if (array [I] > array [j]) {var temp = array [I]; array[i] = array[j]; array[j] = temp; }}}Copy the code

4. The function pays attention to the problem

1. Arguments and arguments are the same
function f1(a){
            arguments[0] = 30;
            console.log(a); //30
            a = 50;
            console.log(arguments); //50
    }
    f1(10);
Copy the code
2, the same name will cover
Function f2(){console.log(" function 1"); } function f2(){console.log(" function 2"); } f2();Copy the code
3, variable function of the same name will be overwritten
Function f3(){console.log("分"); } var f3 = 10; console.log(f3); //f3(); //f3 is not a functionCopy the code
4. Number of arguments Different number of parameters
function f4(a,b){ //a = 10 b = 20 console.log(a+b); } the f4 (10, 30); // The number of arguments is greater than the number of parameters, regardless of f4(10); // The number of arguments is less than the number of parametersCopy the code

5. Scope and variable promotion

1. Scope

Scope: The valid scope of a variable or function

Global variables: Variables/functions declared outside a function, called global variables/functions, are always stored in memory and can be modified and accessed from anywhere

Local variables: variables declared inside a function, called local variables/functions, can only be used inside the function, {} out of the function will be recycled

Scope chain: is a kind of search mechanism in JS, first find oneself, oneself in order to find, until the global, is not defined

Global variables:
var a = 10; console.log(a); //10 function fun(){ console.log(a); a = 100; } fun(); console.log(a); / / 100Copy the code
Local variables:
function fun2(){
         var c = 10;
         console.log(c);
     }
     fun2();
     //console.log(c); //c is not defined 
Copy the code
Scope chain:
var s = 10;
     function out(){
         var s = 20;
         function inner(){
             var s = 30;
             console.log(s);
         }
         inner();
     }

     out();
Copy the code
Variable promotion:

Var a a=undefined function f = function(){} var a=undefined function f = function(){}

/ / 1. Looking for var console. The log (a); //undefined var a = 10; console.log(a); / / 2. To find the function console. The log (f); function f(){ //fdfdfd console.log("ffff"); } console.log(sum); var sum = 10; function sum(){ console.log("fd"); } //sum(); //10 sum is not a functionCopy the code

The internal function is also preparsed before execution

   function sum(a){//var a
        console.log(a);   //undefined
        var a = 10;
      }
      sum(10);
Copy the code

6. Function return value

All functions return a value, undefined by default. Use return to return the specified result. Syntax: return The result to be returned note: Return can only return one value. If you write more than one value, return the last function, as long as return is encountered, the function ends

function sum(a,b){ var s1 = a+b; var s2 = a*b; // return s1,s2; }Copy the code

7. Get non-element styles

Gets a non-line style

Standard browser: getComputedStyle(tag). Attribute Name IE6 7 8 Incompatible with IE: tag. CurrentStyle. Attribute name Standard browser incompatible Compatibility: The compatibility of two methods, with one method as the criterion

if(oDiv.currentStyle){//ie var w = oDiv.currentStyle.width; }else{// standard var w = getComputedStyle(oDiv).width; } alert(w);Copy the code