几个 JavaScript Questions

来源:互联网 发布:狸窝 mac 编辑:程序博客网 时间:2024/05/21 23:58

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: undefined
Sorry, I am not really understand it....


原创粉丝点击