JavaScript (VII) – function expansion

Time:2022-5-28

Function return value

Return you can change the internal “return value” of a function
1. after a function is executed, the default return value is undefined,
 
function x(){

      return 111;
    }
//The function is self executing. The return value is the return value of the function when it is internally running
console.log(  x()  );
Output to page: document Write
function sum(a,b){
      return a + b;
    }
//The actual parameter is passed into the formal parameter, and the return value of the formula is printed
console.log( sum(4, 5) ** 2 );
2. when the function is executed to return, the function ends immediately and will not be executed later
3. the function closest to the function will work, and the following code will not be executed
   function x( a ){
      if (a<10){
        return a;
      }

      return a * 2;
    }

    console.log( x(9) );
    console.log( x(20) );
    console.log( x(30) );

When you click the return value using a named function as a page event, you only need to assign a value to the function name

//Function name assignment
let a = 0;
function fn(){
      a ++;
      console.log(a);
    }

document.onclick = fn;

//Function name self execution
let a = 10
function fn(){
      return function(){
        a ++;
        console.log(a);
      }
    }

document.onclick = fn();

Various this points in the function

//In the global scope, this points to window
    console.log(this);//window


    function x(){
      console.log(this);
    }
    //Variable function self execution, this points to window
    x();//window


    //Function expression self executing, this points to window
    (function(){
      console.log(this);//window
    })();


    //This of the event function points to the node object corresponding to the event
    /*document.onclick = function(){
      console.log(this);
    };*/



    let obj = {
      name : "Fly",
      age : 18,
      say : function(){
        console.log(this);
      }
    };
    //When a method of an object executes itself, this points to the object
    obj.say();


    //However, if it is used as an event function, this still points to the node object according to the rules of the event function
    document.onclick = obj.say;

Modify the direction of this

//Both call and apply are self executing

    let obj = {
      n : "Fly",
      a : 16
    };

    function fn( a,b ){
      console.log(a+b,this);
    }

    // fn();//window
    
    //The first argument passed in, and the formal parameters passed in sequence from now on represent the point of this
    fn.call( obj,2,3 );

    //Pass in an array, corresponding to the formal parameters in turn
    fn.apply( obj,[3,4] );

   //Bind will not be executed actively, but passively
    let obj = {a:1,b:2};


    let fn = function(){
      console.log(this);
    };

    //Bind will not be executed actively, but passively
    document.onclick = fn.bind(obj);

Basic data type: stack memory, complex data type: heap memory

/*let a = 10;
    let b = 10;
    console.log(a === b); //true

    let c = {n:1};
    let d = {n:1};
    console.log(c === d);*///false


    let e = {h:10};
    Let f = E// Reference relation
    // console.log(e === f);//true

    f.g = 20;

    console.log(e);

Typeof data type used to detect data

console.log( typeof 10 ); //"number"
    console.log( typeof "10" ); //"string"
    console.log( typeof true ); //"boolean"
    console.log( typeof undefined ); //"undefined"
    console.log( typeof {} ); //"object"
    console.log( typeof [] ); //"object"
    console.log( typeof window ); //"object"

    //Special
    console.log( typeof null ); //"object"
    console.log( typeof function(){} ); //"function"


    console.log(typeof typeof 10);//"string"

Various pop ups of alert

// alert( 10 );
    //Alert ("hahaha");
    // alert( true );
    // alert( undefined )
    // alert( null )

    //Alert ([1,2,3])// Contents of array 1,2,3
    //Alert ({a:10})// Object becomes a string [object object]

    //Function pop-up window, what you write is what you write
    function fn(){
      let a = 10;
      let b = 20;
      return a+b;
    }
    alert( fn );