几道比较有意思的题目

1.

var a =1;
var json = {
    a:10,
    val:function(){
        alert(this.a*=2);
    }
}
json.val();
var b = json.val;
b();
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(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||"coco";
}
t1.prototype.name = "web";
t2.prototype.name = "web";
t3.prototype.name = "web";
alert(new t1().name +new t2().name+new t3().name);

4.

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

test(20);

结果:

第一题:
20 
2
4
24
第二题:
2
4
14
第三题:
webundefinedcoco
第四题:
4

 

posted @ 2017-10-10 11:03  JSKevin  阅读(320)  评论(0编辑  收藏  举报