递归
1.找规律
2.找出口
预编译
js执行三部
- 语法分析
- 预编译
- 解释一行执行一行
预编译
- 函数声明整体提升
- 变量 声明提升
- imply global 暗示性全局变量 ,即任何变量,如果变量未经声明就赋值,次变量就为全局对象所有。
function f()
{
var a = b = 123;
}
console.log(b);
//123 b是全局变量
- 一切声明的全局变量,全是window的属性。
var a = 123;
window.a = 123;
window就是全局
预编译过程
1.创建AO(Activation Object)对象,执行期上下文
2.找形参和变量声明,将变量和形参名作为AO属性名,值为undefined
AO{
a:undefined;
b:undefined;
}
3.将实参值和形参统一
AO{
// 把1传进来
a:1;
b:undefined;
}
4.在函数体里面找到函数的声明,值赋予函数体。
AO{
a:function a(){};
b:undefined;
d:function d(){};
}
function fn(a){
console.log(a);
// function a(){}
var a = 123;
console.log(a);
// 123
function a(){}
console.log(a);
// 123
var b = function(){}
console.log(b);
// function(){}
function d(){}
}
fn(1);
//预编译发生在函数执行的前一刻
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(){};
}
function test(a,b){
concole.log(a);
// 1
c = 0;
var c;
a = 3;
b = 2;
concole.log(b);
// 2
function b(){};
function d(){};
concole.log(b);
// 2
}
test(1);
全局
1.生成了一个GO对象Global Object
GO {
a:undefined;
}
GO {
a:function a();
}
GO === window
console.log(a);//function a(){}
var a = 123;
function a(){};
console.log(a);//123
GO{
B:123;
}
function test(){
var a = b = 123;
console.log(window.b);//123
console.log(window.a);//undefined
}
test();
AO{
a:undefined;
}
function test(){
console.log(b); //undefined
if(a)
{
var b = 100;
}
c = 234;
console.log(c); //234
}
var a;
test();
a = 10;
console.log(c);//234
1.
function bar(){
return foo;
foo = 10;
function foo(){
}
var foo = 11;
}
console.log(bar()); //function foo(){}
2.
console.log(bar()); //11
function bar(){
foo = 10;
function foo(){
}
var foo = 11;
return foo;
}