This is the 7th day of my participation in the August Text Challenge.More challenges in August
Variable promotion [pre-parsing]
Before the current context code is executed “top-down”, the browser will declare or define all pending “var/function” keywords
- Knowledge with VAR is declared ahead of time
- Function is pre-declared + assigned (definition)
EC(G) variable promotion:
// var a;
// fn1=0x000; [[scope]:EC(G)]
// var fn2
console.log(a); // undefined
fn1();
var a= 10; / / global a = 10
function fn1(){ // Skip [variable promotion truncation already created]
Copy the code
// var a;
// fn1=0x000; [[scope]:EC(G)]
// var fn2
console.log(a); // undefined
fn1();
var a= 10; / / global a = 10
function fn1(){ // Skip [variable promotion truncation already created]
` `'// Can you do this type of problem correctly? `` `js
// EC(FN1)
(fn1),ec(g)>
// Parameter assignment: --
// Variable promotion:
// var a;
console.log('fn1') // 'fn1'
console.log(a); // undefined
var a = 20; / / private a = 20
console.log(a); / / 20
}
fn1();
fn2(); // Uncaught TypeError: fn2 is not a function
var fn2 = function(){ // Function expression
console.log('fn2')
}
fn2();
Copy the code
In real projects, it is recommended to use function expressions to create functions. Normal functions are declared and assigned in the variable promotion phase, while function expressions are only declared
// Function expression can specify the order of function execution.
// const fn = function(){};
// fn();
Copy the code
Anonymization of functions
// Namespaces for anonymous functions
var fn = function sum() {
console.log('OK');
};
fn();
sum();
Copy the code
Self-executing function
"use strict";// -> make js use strict mode
// Self-executing function
(function anonyous(x){
if(x<0) return;
console.log(x);
// recursive call
// console.log(arguments.callee); // The function itself
Uncaught TypeError: "Use strict" is not a functio
// arguments.callee(x-1)
anonyous(x-1); }) (10)
Copy the code
Variable lifting function
// EC(G)
// Variable promotion:
// fn=0x000 [[scope]:EC(G)]
// [in older browsers]
// sum=0x001 [[scope]:EC(G)]
// [in newer browsers]
// sum;
console.log(sum,fn); / / undefined ƒ fn () {}
function fn(){}
if(1! = =1) {function sum(){}}Copy the code
I see a long way to go, I will search up and down, refueling together, learn the front end!