解决JavaScript中构造函数浪费内存的问题!
把构造函数中的公共的方法放到构造函数的原型对象上!
// 构造函数的问题!
function Gouzaohanshu(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
// this.hanshu = function() {
// console.log(123)
// }
}
// 把构造函数放到咱们的原型对象身上!
Gouzaohanshu.prototype.hanshu = function () {
console.log(123)
}
var gz = new Gouzaohanshu('lvhang', 23, 'nan');
var gz2 = new Gouzaohanshu('lvhang', 23, 'nan');
console.log(gz.hanshu() === gz2.hanshu()) // true
console.dir(Gouzaohanshu)
// 通常状况下, 咱们的公共属性定义到构造函数里面! 公共的方法咱们放到原型对象身上!
</script>