你真的已经搞懂JavaScript了吗?

在Baranovskiy博客中看到一篇So, you think you know JavaScript?的博客,http://dmitry.baranovskiy.com/post/91403200

Quick test for real understanding of JavaScript core beyond closures and scopes. Here five small scripts. Try to answer what will be alerted in each case without running them in the console. Then you could create a test file and easily check your answers. Ready?

if (!("a" in window)) {
    var a = 1;
}
alert(a);

//这个应该是预编译造成的
var a = 1,
    b = function a(x) {
        x && a(--x);
    };
alert(a);

//理解两点:1、预编译期完成了函数声明和变量声明;2、预编译期函数声明会覆盖变量声明,但执行期变量赋值,而函数没有执行。
function a(x) {
    return x * 2;
}
var a;
alert(a);

//这个我觉得跟上面那题一样,
预编译期函数声明会覆盖变量声明
function b(x, y, a) {
    arguments[2] = 10;
    alert(a);
}
b(1, 2, 3);

//这个需要理解arguments对象
function a() {
    alert(this);
}
a.call(null);

//这个比较简单了,平常经常用到,相当于a.call(window)

 

posted @ 2013-05-10 12:59  人 在 旅 途  Views(186)  Comments(0)    收藏  举报