// Precompilation occurs just before the function is executed
The original type
function fn(a){ console.log(a); var a = 123; // Variable declaration console.log(a); Function a (){} console.log(a); Var b = function(){} console.log(b); Function d(){} fn(1);Copy the code
1. Create an AO Activation Object, a library of storage space generated by the function
AO{
}
Copy the code
2. Find the parameter and variable declaration with undefined(use the parameter name as the AO attribute name)
AO{
a:undefined,
b:undefined,
}
Copy the code
3. Put the argument values inside the parameters
AO{
a:1,
b:undefined,
}
Copy the code
4. Look in the function bodyFunction declaration, the value is assigned to the function body
AO{
a:function a() {},
b:undefined,
d:function d() {}
}
Copy the code
After the precompiled
function fn(a){ console.log(a); / / -- -- -- -- -- -- -- -- -- -- print of a is the inside of the AO a: function () {}, var a = 123; console.log(a); // the printed a at -------- is the real stored value, AO{a:123, b:undefined, d:function d() {}} function a() {} console.log(a); / / -- -- -- -- -- -- -- -- -- -- -- -- print 123 var b = function () {} AO {a: function a () {}, b: function () {}, d: function d () {}} to the console. The log (b); / / -- -- -- -- -- -- -- -- -- -- -- -- -- print b: function () {}, function d () {}} fn (1);Copy the code
example
Function test(a,b) {// Call console.log(a) for the first time; //-------1 c = 0; var c; // first access a = 3; // do not print b = 2; console.log(b); / / -- -- -- -- -- -- -- -- -- -- - 2 function b () {} / / for the first time to visit the function () {} d / / first visit to the console. The log (b); //-----------2 } test(1);Copy the code
Precompilation process
AO{
a:undefined,
b:undefined,
c:undefined,
}
AO{
a:1,
b:undefined,
c:undefined,
}
AO{
a:1,
b:function b(){},
c:undefined,
d:function d(){},
}
Copy the code
perform
AO {a: 1, b: function () {} b, c: 0, d: function d () {},} AO {3, a: (didn't call out) b: 2, c: 0, d: function d () {},}Copy the code
example
function test(a,b) { console.log(a); //function a(){} console.log(b); //undefined var b = 234; console.log(b); //234 a = 123; console.log(a); //123 function a() {} var a; b = 234; var b = function() {} console.log(a); //123 console.log(b); //function() {} } test(1);Copy the code
Precompile the last step
AO{
a:function a(){}
b:undefined,
}
Copy the code
Start to perform
Precompilation occurs not only in the function system, but also globally. If a variable is assigned an undeclared value, it is placed in the global GO
1. Generate a GO Object, Global Object //GO====window
example
Type b
function test() { var a = b = 123; console.log(window.b); //----------123 } test();Copy the code
Print a
function test() { var a = b = 123; console.log(window.a); //---undefined } test();Copy the code
example
console.log(test);
function test(test) {
console.log(test);
var test = 234;
console.log(test);
function test() {
}
}
test(1);
var test = 123;
Copy the code
AO{ test:234 }
example
var global =100;
function fn() {
console.log(global);
}
fn();
Copy the code
GO{ global:100, fn:function(){... }} global=100;Copy the code
example
GO{first step: global:undefined; Global :100} global = 100; function fn() { console.log(global); //undefined global = 200; console.log(global); //200 var global = 300; // the AO variable declaration changes to undefined; } // AO{ // global:undefined; // } fn() var global;Copy the code
example
function test() { console.log(b); //undefined if(a) { var b = 100; } console.log(b); //undefined c = 234; console.log(c); //234 } var a; test(); // the function starts executing AO a = 10; console.log(c); / / 234Copy the code
// AO{
// b:undefined
// }
Copy the code
Example 1
function bar() { return foo; ====function foo() {} foo= 10; function foo() { } var foo = 11; } console.log(bar())Copy the code
Example 2
console.log(bar()); ======11 function bar() { foo = 10; function foo() { } var foo = 11; return foo; }Copy the code
example
console.log(b); Var b = function(){Copy the code
Example (2, undefined undefined, 10100123)
GO{
a:100;
demo:function(){}
f:123
}
Copy the code
a = 100; function demo(e) { function e() {} arguments[0] = 2; console.log(e); //2 if(a) { var b = 123; Function c() {}} var c; a = 10; var a; console.log(b); //undefined f = 123; console.log(c); //undefined function console.log(a); //10 } var a; demo(1); console.log(a); //100 console.log(f); / / 123Copy the code
AO{
e:2
b:undefined,
c:undefined,fnc,
a:10
}
Copy the code
Hard example implicit type conversion
var str = false + 1; document.write(str); //1 var demo = false == 1; document.write(demo); / / false if (typeof (a) / / && - true + + "") (+ undefined {# # # # # # / / the string" undefined "&" NaN ", Boolean document. Write (" learned "); } the if (11 + "11" * 2 = = 33) {document. Write (" learned "); }// both sides of the multiplier are converted to numeric types!!" "+!!!!!" "-!!!!! False | | document. Write (" learn waste ") + false true false = 1 or operator find true is returned, not go backCopy the code
example
(window.foo || (window.foo = "bar")); Window. foo = "bar"// read the parentheses firstCopy the code