var new_object = function(){
this.a = a;
this.b = b;
this.addfunction = function(){
return this.a + this.b;
};
}
var obj = new_object;
obj.a = 2;
obj.b = 3;
var count = obj.addfunction();
alert(count);报错:TypeError: obj.addfunction is not a function
this.a = a;
this.b = b;
this.addfunction = function(){
return this.a + this.b;
};
}
var obj = new_object;
obj.a = 2;
obj.b = 3;
var count = obj.addfunction();
alert(count);报错:TypeError: obj.addfunction is not a function
this.a = 0;
this.b = 0;
this.addfunction = function(){
return this.a + this.b;
};
}
var obj = new new_object();
obj.a = 2;
obj.b = 3;
var count = obj.addfunction();
alert(count);......
改成 var obj = new new_object();
声明一个变量new_object,再声明一个变量obj并赋值为new_object,再对obj添加两个属性a和b,再调用obj的一个没有声明的方法,关键是填写代码的区域根本不会执行,怎么让返回结果为5?!
var that ={
a:0,
b:0,
addfunction:function(){
return this.a+this.b
}
};
return that;
}
/*************************************************
这里要改题目加上()才能执行。
*/
var obj = new_object();obj.a = 2;
obj.b = 3;
var count = obj.addfunction();
console.log(count);
这其实是个脑筋急转弯,答案是:/**填写代码*/
}
new_object.a=0;//静态变量
new_object.b=0;//静态变量
new_object.addfunction=function(){//静态方法
return new_object.a+new_object.b;
// ----------------------------- begin 填写代码
};new_object.addfunction = t;function t() {
return this.a + this.b;
// ----------------------------- end 填写代码
}var obj = new_object;
obj.a = 2;
obj.b = 3;var count = obj.addfunction();
alert(count);