<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
/*
重写tostring
*/
function F(name,age,gender){
this.name=name;
this.age=age;
this.gender=gender;
}
var fun1=new F("孙悟空",18,"男");
var fun2=new F("猪八戒",28,"男");
// 重写前的toString()
console.log(fun1.toString());
console.log(fun1.__proto__.__proto__.hasOwnProperty("toString"));
// 重写后toString()
F.prototype.toString=function(){
return [this.name,this.age,this.gender];
}
console.log(fun1.toString());
console.log(fun2.toString());
</script>
</head>
<body>
</body>
</html>
08 重写——js
最新推荐文章于 2024-11-13 01:18:00 发布