这篇文章主要说明一下javascript 底层对变量和函数是怎样存在和运算的。
众所周知 javascript 中一切都是对象,此处要说的就是OA。
首先说明作用域的问题
<script type="text/javascript">
var str1 = 'global';
function testOne(){
alert(str1);
alert(str2);
var str2='local';
}
testOne();//global,underfined
</script>
分析如下
testOne 在调用时没有输出 global 和 local 因此js内部存在一个过程分析。
当函数运行的一瞬家主要分析3各方面。
1.参数;
2. 局部变量声明;
3.函数声明;
此时引入激活对象。
案例:
function t(){
function x(){
alert(x);
}
x();
}
t();//function x(){
alert(x);
} 此时输出函数
function sop(x,y){
var x=56;
alert(x);
alert(y);
}
sop(2,3)//56,3
运行分析:
function t3(x,y){
var x=7;
console.log(x);//7
console.log(y);//undefined
}
t3(5);
1.分析参数
AO[x,y]
2. 分析变量声明;
AO.x 已存在,不做影响
3 .赋值结果AO[x,y]
执行过程
ao.x = 7
console.log(AO.X)==>7
console.log(AO.y)==undefind
例二::
var str1 = 'glo';
function t4(){
console.log(str1);
console.log(str2);
var str2 = 'local';
}
t4();
赋值分析
1 AO[];
[ao[str2]],值为underfind
执行
console.log(str1); //ao 上找不到,到全局找;
console.log(str2);//ao上找到,值是underfined