JS foundation 006 --- short circuit assignment, variable scope, JS precompiling, function declaration method, function recursive call

Posted by richtom80 on Mon, 13 Dec 2021 13:07:03 +0100

1, Short circuit assignment

Principle: || seeing the truth in the operation is the real short circuit
For example: var a = B | C
If b is true, the value of b will be paid to a; If b is false, assign the value of c to a
*Note: the following values in js will be converted to false:

    false,undefined,null,0,NaN," ",' '

2, Scope of variable

Scope: the scope of action. Or effective range.

  • Local variable: a local variable is a variable defined inside a function. This variable can only be used inside a function, that is, the scope is only inside the function. In addition, formal parameters are also local variables.

  • Global variables: variables declared outside the function and variables without var. this variable is valid in any function, that is, the scope is anywhere in the current file.
    *Note: local variables and global variables should not have the same name

//Variable can be declared repeatedly
//Variables can be assigned directly without declaration
//When the internal variable name of a function is the same as that of an external variable, it is preferred to use its own - different people with the same name

Chestnuts:

		var a = 4 ;
        console.log(a);  // 4

        var a = 5 ;   // 5

        b = 6 ;
        console.log(b);
		function fn() {  
            // Variables are declared inside a function and can only be used inside a function
            var c = 9 ;
            console.log(c);
            // Without var, this variable defaults to a global variable
            d = 10 ;
            var b = 20 ;
            console.log(b);  // 20
        }
        
        fn()

        // console.log(c);  //  Error C is a local variable

        console.log(d);

        console.log(b);  // 6

*A formal parameter is essentially a local variable

3, Precompiling of js

The execution of js code is divided into two stages:

  1. Precompile phase: first, all variables declared by var and all named functions will be found
  2. Code execution phase

For example:
Code you see:

		// The code you see
        console.log(a);    // undefined

        var a = 10 ;

        console.log(a);   // 10

In fact, js parsing:

		var a ;
        console.log(a);
        a = 10 ;
        console.log(a);

Code you see:

		console.log(a);
        var a = 10 ;
        a()
        function a() {  
            console.log(3);
            var b = 4 ;
        }
        console.log(b);
        var b = 5 ;
        console.log(b);
        console.log(a);

In fact, js compilation:

		var a ; 
        var b ;
        function a() { 
            // There is also precompiling inside the function
            var b ; 
            console.log(3);
            b = 4 ;
        }
        console.log(a);  // f
        a = 10 ;
        a() ;    // a is not a function program reports an error. So far, it will not go down
        console.log(b);  
        b = 5 ;
        console.log(b);  

4, How functions are declared

  1. Named function - there is a problem of precompiling when there is a name
  2. There is no precompiling problem for assignment functions:
    variable
    Data type: basic data type (5) + reference data type ()
		// Assignment function
        var fn2 = function () {  
            console.log(777);
        }
        fn2()
        console.log(typeof fn2);  // function
  1. Anonymous function ---- one-time function
		// Self calling of anonymous functions -- one-time functions
        ~function (n) {  
            console.log(n);
        }(666)

####Several system functions:

  1. prompt( )
		// Pop up window with input box
        var res = prompt('Please enter your name');
        console.log(res);
  1. confirm( )
		// OK pop-up
        var res = confirm('Are you sure you want to delete me');   // Returns a Boolean value
        console.log(res);

3.eval( )

		// eval() is calculated when it can be calculated. If it cannot be calculated, an error is reported directly
        console.log(eval('1 + 2a'));

		function calc(a , b , f) {  
            return eval(a + f + b)
        }

        var res = calc(1,2,'+');
        console.log(res);

5, Recursive call of function

Concept:
Transfer and regression functions can also call themselves, which is called recursive call

importance:
Recursion is a component of recursion. Recursion belongs to the knowledge that is difficult to understand in functions. In application development, although it is not used very frequently, it reflects your skills. Moreover, it is best to be able to recursion when engaged in IT industry development. If you can not use it flexibly now, you must be able to do it in the company in the future. If someone asks you about recursion in the interview, please explain, He is very demanding of you.

Chestnuts:

		function fn() {  
            fn()
        }
        fn()   // Dead recursion

This recursive call does not end and will continue to be called, similar to an dead loop

Here is a simple recursive call:
Find 1 + 2 + 3 + 4 +... + 100;

Analysis: 1+2+3+4+...+100 ;
	  100 + (Sum of the first 99 numbers)
	       99  + (Sum of the first 98 numbers)
			 98 +  (Sum of the first 97 numbers)	 
				         ...
				         
                      2 + (Summation of the previous number)

realization:

		function sum(n) {  
            
            if(n == 1){
                return 1  //End call when n=1
            }
            return n + sum(n-1)
        }
        sum(5)
        //   5 + sum(4)
        //        4 + sum(3)
        //            3 + sum(2)
        //                 2 + sum(1)
        //                      1

Fiborache sequence 1 1 2 3 5 8 13 21 34
Find the nth number (n-1) + (n-2)
The law of fiborache sequence is that the sum of the first two numbers is equal to the third number
Implementation code:

		function fb(n) {  
            if(n == 1 || n == 2) {
                return 1
            }
            return fb(n - 1) + fb(n - 2)
        }

        console.log(fb(6));

Topics: Javascript Front-end