构造函数的原型
prototype 原型对象
原型对象的作用就是共享方法
function Star(uname, age) {
this.uname = uname;
this.age = age;
/* this.sing = function () {
console.log("我会唱歌,我会跳舞");
}; */
}
Star.prototype.sing = function () {
console.log("我会唱歌,我会跳舞");
};
var ldh = new Star("刘德华", 18);
var zxy = new Star("张学友", 19);
console.log(ldh.sing == zxy.sing); //false 使用原型对象后是true
一般情况,我们的公共属性定义到构造函数里面,公共的方法我们放到原型对象身上
构造函数方法new一次就开辟一次内存空间,浪费资源内存
浙公网安备 33010602011771号