阿里巴巴 2015 暑期实习前端笔试题(部分)

第一题:

var a = 0;
function A(){
    this.a = 1; 
    setTimeout(function(){
        this.a = 2;
        try{
            this.b="b";
            throw '';
        }
        catch(e){
            this.b='bb';
        }
    },0);
    this.b="bbb";
}

var aa = new A();

setTimeout(function(){
    console.log(aa.a);
    console.log(window.a);
    console.log(aa.b);
    console.log(window.b)
},0);

1、2、bbb、bb;

考察点应该是setTimeout()的作用域是全局对象,在浏览器中,即为 window。

第二题:

不借助第三方库,请编码完成:给一个超链接元素绑定一个鼠标单击的事件。要求单击该元素后,提示用户“用户名不能为空”。(注意:请尽可能兼容给多的浏览器)

考察原生 js 事件绑定,在JavaScript高级程序设计那本书的事件章节有详细讲述。不过话说有空去看看 jQuery 那货是怎么做的肯定大有帮助。

扫描二维码关注公众号,回复: 316676 查看本文章
<a id= "hyper" href="#">我是一个超链接</a>

 

var a = document.getElementById("hyper"),
handler =function(e){
    alert("用户名不能为空");
}
if (window.addEventListener){
    a.addEventListener("click",handler,false);
}
else if(window.attachEvent){
    a.attachEvent("onclick",handler);
}
else{
    a["onclick"]=handler;
}

 

猜你喜欢

转载自specterxu.iteye.com/blog/2357329