js--打卡--11.27 toString方法的改造。

来源:互联网 发布:mysql攻击全攻略 编辑:程序博客网 时间:2024/05/17 06:45
<script>//创建一个构造函数function Person(name,age,gender){this.name= name;this.age = age;this.gender = gender;}//创建一个Person类的实例var per = new Person("哈哈",11,"男");console.log(per);    //"[object Object]"console.log(per.toString()); //"[object Object]"//toStirng方法属于console.log(per.hasOwnProperty("toString")); //falseconsole.log(per.__proto__.hasOwnProperty("toString")); //falseconsole.log(per.__proto__.__proto__.hasOwnProperty("toString")); //true//我想要改变toSting()得到的内容。Person.prototype.toString = function(){return ("name="+this.name+", age="+this.age+", gender="+this.gender);}console.log(per); //Person.prototype.toString,在构造函数的原型对象中修改toSting方法。通用方法。var per1 = new Person("嘿嘿",15,"女");console.log(per1);</script>