日期:2014-05-16 浏览次数:20574 次
//类
function abc(a, b, c) {
this.result = a + b + c;
}
var obj = new abc(1,2,4);
alert(obj.result);
//函数
function abcd(a, b, c, d) { return a + b + c + d; }
alert(abcd(1,2,3,4));
------解决方案--------------------
你需要重写prototype.toString方法
下面代码实现了你的需要
function abc(){
this.r = 0;
for(i=0;i<arguments.length;i++)
this.r+=arguments[i]
}
abc.prototype.toString=function(){return this.r;}
var obj=new abc(1,2,4);
alert(obj)
var obj2=new abc(1,2,4,6,8,9,80);
alert(obj2)
------解决方案--------------------
alert 会调用参数对象的toString()方法。
重写下toString 就O了。