Javascript 本身就有很多独特的语言特性。由此,相关的问题也很有意思。接下来,我列举几个,并简单分析一下。
我是在chrome下测试的。。。
var a = 1,
b = function a(x) {
x && a(--x);
};
alert(a);
Answer: 1.
Analyze: functiona() just anonymous function, and it is equal to function b(), the scope of functiona() is only in function b(). so a just one variable in function alert.
function a(x) {
return x * 2;
}
var a;
alert(a);
Answer:
functiona(x) {
return x*2;
}
Analyze: Forvariable name hoisting, variable a is declaration before function a(). So a is just one function in function alert.
function b(x, y, a) {
arguments[2] = 10;
alert(a);
}
b(1, 2, 3);
Answer: 10
Analyze: it iseasy, local variable a is changed by arguments[2] in function b.
function a() {
alert(this);
}
a.call(null);
Answer: object window.
Analyze: it issame as a().
For other cases: 1. a.call(undefined); output: object window.
2. a.call(0); output: 0
if (!("a" in window)) {
var a = 1;
}
alert(a);
Answer: undefinedSorry, I am not really understand it....