作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.JavaScript源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>toString</title>
<script type="text/javascript">
function Person(name,age,address) {
this.name = name;
this.age = age;
this.address = address;
}
Person.prototype.toString = function(){
return "<Person name = " + this.name + ", age = " + this.age + ", address = " + this.address + ">";
}
Person.prototype.arms = "冲锋枪";
var p1 = new Person("孙悟空",500,"花果山");
var p2 = new Person("如来佛祖",1000,"大雷音寺");
/**
* 当我们直接在页面中打印一个对象时,实际上是输出对象的toString()方法的返回值。
*/
console.log(p1);
console.log(p1.toString());
console.log("result = " + p1);
console.log("result = " + p1.toString());
console.log(p2);
console.log("result = " + p2);
console.log("result = " + p2.toString());
</script>
</head>
<body>
</body>
</html>
二.浏览器打开以上代码渲染结果