当我们直接在页面中打印一个对象时,事件上是输出的对象的toString()方法的返回值
如果我们希望在输出对象时不输出[object object],可以为对象添加一个toString()方法
function fun(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
}
var two=new fun("孙悟空",18,"男");
var three=new fun("猪八戒",18,"男");
two.toString=function(){
return "fun=[name="+this.name+",age="+this.age+",sex="+this.sex+"]";
}
var res=two.toString();//记得打括号!!!!!!!!!!
console.log(res);
查找toString在第几层
console.log(two);
two.toString=function(){
return this.name;
}
var res=two.toString();
console.log(res);
console.log(two.__proto__.__proto__.hasOwnProperty("toString"));
console.log(res);