首页 > 代码库 > 这几道题,你会做吗?

这几道题,你会做吗?

1.考点:上下文

var a =1;
var json = {
a:10,
val:function(){
alert(this.a*=2);
}
}
json.val();
var b = json.val;
b();
alert("stop-stop")
json.val.call(window);
alert(window.a + json.a);

2.考点:属性

var a =1;
var json = {
a:10,
val:function(){
alert(a*=2);
}
}
json.val();
var b = json.val;
b();
alert("stop-stop")
alert(window.a + json.a);

3.考点:原形链

function t1(name){
if(name) this.name = name;
}
function t2(name){
this.name = name;
}
function t3(name){
this.name = name||"test";
}
t1.prototype.name = "yupeng";
t2.prototype.name = "yupeng";
t2.prototype.name = "yupeng";
alert(new t1().name +new t2().name+new t3().name);

4.考点:闭包

var test = (function(a){
return function(){
alert(a*2);
}
})(2);

test(20);

这几道题,你会做吗?