首页 > 技术文章 > Javascript原型对象中的toString

lw1998 2019-08-22 14:02 原文

<script>
//tostring
function Person(name,age,gender){
this.name=name;
this.age=age;
this.gender=gender;
};

//当我们直接在页面中打印一个对象时,实际上是输出的对象的tostring()方法的返回值;
//console.log(per.__proto__.__proto__.hasOwnProperty("toString"));
//返回ture
Person.prototype.toString=function(){
//通过修改原型对象内容
return "name:"+this.name+",age:"+this.age+",gender:"+this.age;//返回自己定义的内容
};

var per=new Person("孙悟空",18,"男");
console.log(per);
</script>

推荐阅读