请写出下面程序的输出结果是什么,并写出为什么;分析的过程是什么
function fun(num){
console.log(num); //3 实参形参统一
var num = 5;
console.log(num);//5
}
fun(3)
/*
AO{
num:undefined---->>3------>>5
}
*/
function fun(ger){
console.log(ger); //function ger(){alert("hello world")}
function ger(){
alert("hello world");
}
}
fun(5)
/*
AO{
ger:undefined
}
AO{
ger:5
}
AO{
ger:function ger(){alert("hello world")}
}
}
*/
function fun(ger){
console.log(ger); //5
var ger = function(){//函数表达式
alert("hello world");
}
}
fun(5)
/*
AO{
ger:undefined
}
AO{
ger:5
}
*/
function a(b){
console.log(b); //function b(){console.log(b);}
function b(){
console.log(b); //function b(){console.log(b);}
}
b();
}
a(1)
/*
AO{ //创建AO对象
}
AO{ //变量提升
b:undefined
}
AO{ //形参实参相统一
b:1
}
AO{ //函数提升
b:function b(){console.log(b);}
}
*/
function a(b){
console.log(b); //1
var b = function(){//函数表达式
console.log(b);
}
b();
}
a(1)
/*
AO{
b:undefined
}
AO{
b:1
}
AO{
b:function (){console.log(b);}
}
*/
function test(a,b){
console.log(a); //a(){} function
console.log(b); //undefined
var b = 234; //undefined--->>234
console.log(b); //234
a = 123; //function------>123
console.log(a); //123
function a(){};
var a ;
b = 234;
var b = function(){}; //234---function
console.log(a); //123 向上找最近的赋值
console.log(b); //function{} 向上找最近的赋值
}
test(1);
//1、创建ao空对象
//2、形参提升:
AO{
a:undefined //形参
b:undefined //形参
}
//变量提升
AO{
b:undefined;
a:undefined;
}
//3、形参实参统一
AO{
a:1
b:undefined
}
//4、函数提升
AO{
a:1
b:undefined
}
AO{
a:function
b:undefined
}
//解析执行
console.log(test);//function test(test){...}
function test(test){
console.log(test);//function test(){}
var test = 234;
console.log(test); //234
function test(){}
}//函数执行结束,AO销毁
test(1); //创建AO对象
var test = 123;
GO{}
GO{
test:undefined;
}
GO{
test:function
}
//创建AO对象
AO{}
AO{
test:undefined
}
AO{
test:undefined
}
AO{
test:1
}
AO{
test:function
}
//解析执行
function test(){
console.log(b); //undefined //undefined
if(a){ // 没有var 是 使用变量
var b = 100;
}
c = 234; // 本质作用仅有赋值 如果全局都没有声明,则含有隐式声明的作用
console.log(c); //234 //234
}//函数执行结束 AO销毁
var a; undefined-----undefined
test();
a = 10; undefined-----10
test();
console.log(c); //234
//1、创建GO空对象
GO{}
//2、变量声明提升 赋值undefined
GO{
a:undefined
}
//3、函数声明提升 赋值function引用
GO{
a:undefined,
test:function
c:234
}
//1、创建AO空对象
AO{}
//2、形参声明提升
AO{
}
//2、变量声明提升(只要有var 都是当前作用域下的变量)
AO{
b:undefined
}
//3、形参实参相统一
//4、函数声明提升并赋值 函数引用
//方法体解析执行
总结
GO执行步骤
- 创建GO空对象
- 找变量声明,将变量名作为GO对象里的属性名,值为undefined
- 全局中找函数声明,赋值函数体
AO执行步骤
- 创建AO空对象
- 找形参和变量声明,将变量和形参名 作为AO里的属性名,值为undefined
- 实参值 和 形参 相统一
- 函数体内找找函数声明,值赋予函数体