5 - parameter default value, implied global variable, precompiled

Keywords: Javascript Front-end ECMAScript

(1) . function parameter default value

Initialization parameter. If it is not set, the default value of the parameter is undefined

function test(a, b){
	console.log(a);
	console.log(b);
}
test(1);  
// 1
// undefined

Do not pass arguments, set default values for row parameters

Only es6 supports formal parameter assignment

function test(a = 1, b = 2){
	console.log(a);
	console.log(b);
}
test();
// 1
// 2

Assign a value to b in the argument to keep a at its default value

function test(a = 1, b){
	console.log(a);
	console.log(b);
}
test(undefined, 2);
// 1
// 2

es5 how to assign default values to formal parameters

function test(a,b){
   var a = arguments[0] || 1;
   var b = arguments[1] || 2;
   console.log(a,b); // 1 2 
}
test();

//The following way of writing is also OK
// typeof prints out strings
function test(a,b){
   var a = typeof(arguments[0]) !== 'undefined' ? arguments[0] : 1;
   var b = typeof(arguments[1]) !== 'undefined' ? arguments[1] : 2;
   console.log(a,b);  //1 2
  }
 test();

(2) , implied global variables

imply global variable

window is a JavaScript built-in object

The variable is assigned without being declared. The variable is an implied global variable, and its ownership belongs to the global object window,

All global variables belong to window.

var a = 1;
b = 2; // Implied global variable
console.log(window.b);

(3) . precompiling:

The following statements will report errors, and none of them will be executed.

The javascript engine will:

1. Checking for grammatical errors throughout the text does not explain the implementation immediately.

1.5 pre compilation process

2. Explain one line, execute one line

console.log(a);
console.log(a);// Chinese semicolon
console.log(a);

Phenomenon 1

If you put the function execution before the function definition, you can still execute the function

test();
function test(){
	console.log(1);
}k,

Phenomenon 2

Print undefined regardless of whether a is assigned or not

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

No declaration will report an error, which means that the declaration is very critical

console.log(a); // report errors

Based on the above phenomena, it is concluded that:

The function declaration is promoted as a whole, the variable declaration is promoted only, and the assignment is not promoted

1. Function precompiling

Function precompiling is the step to be performed before the function is executed.

First, create an AO object. AO activation object is also called function context

(1) , find formal parameters and variable declarations of functions

(2) Assign the parameter value of the argument to the formal parameter

(3) Find the body of the assignment function of the function declaration

(4) . execution function

function test(a){
	console.log(a);
	var a = 1;
	console.log(a);
	function a(){}
	console.log(a);
	var b = function(){}
	console.log(b);
	function d(){}
}
test(2);

(1) , find formal parameters and variable declarations of functions

AO = {
	a: undefined,
	b: undefined
}

(2) Assign the parameter value of the argument to the formal parameter

AO = {
	a: undefined -> 2,
	b: undefined
}

(3) Find the body of the assignment function of the function declaration

AO = {
	a: undefined -> 2 -> function a(){},
	b: undefined
	d: function d(){}
}

(4) . execution function

AO = {
	a: undefined -> 2 -> function a(){} -> 1,
	b: undefined -> function(){}
	d: function d(){}
}
Print results:
f a(){}
1
1
f(){}
For example
function test(a, b){
   console.log(a);
   c = 0;
   var c;
   a = 5;
   b = 6;
   console.log(b);
   function b(){}
   function d(){}
   console.log(b);
}
test(1);

(1) , find formal parameters and variable declarations

AO = {
	a: undefined,
	b: undefined,
	c: undefined,
}

(2) Assign an argument to a formal parameter

AO = {
	a: undefined -> 1,
	b: undefined,
	c: undefined,
}

(3) , find the function declaration and assign the function body

AO = {
	a: undefined -> 1,
	b: undefined -> function b(){} 
	c: undefined 
	d: undefined -> function d(){}
}

(4) . function execution

AO = {
	a: undefined -> 1 -> 5
	b: undefined -> function b(){} -> 6
	c: undefined -> 0
	d: undefined -> function d(){}
}
Print results:
1
6
6

2. GO global context

Actually GO === window

1. Find variable declaration

2. Find function declaration

3. Execute

var a = 1;
function a(){
	console.log(2);
}
console.log(a);

(1) , find variable declaration

GO = {
	a: undefined
}

(2) , find function declaration

GO = {
	a:undefined -> function a(){}
}

(3) . implementation

GO = {
	a:undefined -> function a(){} -> 1
}

Print results:

1
give an example
console.log(a, b);
function a(){}
var b = function(){}

(1) , find variables

GO = {
	b: undefined
}

(2) , find function declaration

GO = {
	b: undefined
	a: function a(){}
}

(3) . implementation

GO = {
	b: undefined
	a: function a(){}
}

Print results

f a(){} undefined
Example 1
function test(){
	var a = b = 1;
	console.log(a);
}
test();

Execution steps:

First, create the global context GO

GO = {

}

When you see the function, create the function context AO

AO = {
	
}

(1) , find formal parameters and variables

AO = {
	a: undefined
}

(2) Assign an argument to a formal parameter. There is no

(3) . find the assignment function body of the function declaration. There is no

(4) . execute the function, b is not declared, and it is hung on GO

GO:{
	 b:1
}

Assign the value of b to a

AO = {
	a: undefined -> 1
}
Example 2
var b = 3;
console.log(a);
function a(a){
	console.log(a);
	var a = 2;
	console.log(a);
	function a(){}
	var b = 5;
	console.log(b);
	
}
a(1);
GO = {
  b: undefined -> 3
	a: function a(a){}
}
AO = {
	a: undefined -> 1 -> function a(){} -> 2
	b: undefined -> 5
Print results:
 function a(a){}
 function a(){}
 2
 5
Example 3
a = 1;
function test(){
	console.log(a);
	a = 2;
	console.log(a);
	var a = 3;
	console.log(a);
}

test();
var a;

(1) , find variables

GO = {
	a: undefined
}

(2) , find function declaration

GO = {
  a: undefined
	test: function test(){}
}

(3) , find formal parameters and variables

AO = {
 a: undefined
}

(4) . the argument is assigned to the formal parameter. There is no parameter here

(5) . find the function declaration and assign the function body. There is no

(6) . implementation

GO = {
  a: undefined -> 1 
	test: function test(){}
}
AO = {
 a: undefined  ->2 -> 3
}

Print results:

undefined // If you have it, you don't GO to GO to find it, so you don't print 1
2
3
Example 4:
function test(){
	console.log(b);
	if(a){
	 var b = 2;
	}
	c = 3;
	console.log(c);
}

var a;
test();
a = 1;
console.log(a);
GO = {
	a: undefined -> 1
	test: function test(){}
	c: 3
}

AO = {
	b:undefined 
}

Print results:

undefined
3
1
Practice 1:
function test(){
  return a;
  a = 1;
  function a(){}
  var a = 2; 
}
console.log(test())
AO = {
	a: undefined -> function a(){} 
}

Execution results:

f a(){} 
Practice 2:
 function test(){
     a = 1;
     function a(){}
     var a = 2;
     return a;
  }
  console.log(test());
AO = {
	a:undefined -> f a(){} -> 1 ->2
}

Execution results:

2
Practice 3:
a = 1;
function test(e){
   function e(){}
   arguments[0] = 2;
   console.log(e);
   if(a){
     var b = 3;
   }
   var c;
   a = 4;
   var a;
   console.log(b);
   f = 5;
   console.log(c);
   console.log(a);
}
var a;
test(1);
console.log(a);
console.log(f);
GO = {
	a: undefined -> 1 
	test: function test(e){}
	f: 5
}
AO = {
	e:undefined -> 1 -> function e(){} -> 2
	b:undefined 
	c:undefined	
	a:undefined -> 4
}

Print results:

2
undefined
undefined
4
1
5

Some exercises:

var a = false + 1;
console.log(a); // 1
var b = false == 1;
console.log(b); // false
if(typeof(a) && (-true) + (+undefined) + ''){
    console.log("Yes");
}else{
    console.log("Failed");
}
// Yes
// typeof(a) is' undefined '
// (- true) is - 1
// +undefined is NaN
// NaN - 1 + '' is' NaN '
if(1 + 5 * '3' === 16){
	console.log('Yes');
}else{
	console.log('Fail');
}
// Yes
console.log(!!' ' + !!'' -!!false || 'Fail'); // 1

Posted by airwinx on Sat, 04 Dec 2021 20:12:29 -0800