alert(nf);     // 5
为什么alert弹出的是5呢?
而console.log(nf);  却是numCount
我把numCount.prototype.toString改成numCount.prototype.toString1
alert就弹出[object Object]..
这是什么原因啊?
function numCount(){
var answer = 0;
}
numCount.prototype.addNumbers = function(num1,num2){
this.answer = num1 + num2;
}
numCount.prototype.toString2 = function(){
return this.answer;
}
var nf = new numCount();
nf.addNumbers(2,3);
console.log(nf);  // numCount
alert(nf);     // 5

解决方案 »

  1.   

    alert 打印 参数的tostringCONSOLE.LOG 比较复杂 应该是直接在控制台输出对象本身
      

  2.   

    问题出在toString2上面,可能会跟Object原型里的toString混淆,最好还是换个名字。
      

  3.   

    把numCount.prototype.toString2改成numCount.prototype.toString
    alert弹出的是5?
      

  4.   

     alert(nf)时会把numCount.第一个有返回值的属性输出来吧
    所以是toString2