function tab(name,age){
this.age=age;
this.name=name;
this.content=function(){//有点低效
return this.name;
}
}
tab.prototype.init=function(){//原型方法
return this.age;
}
var tab=new tab("haha","18")
console.log(tab.init())
console.log(tab.content())
//每次新建一个实例都会在内存中创建content(),有点低效,解决方法将这些可以共享的方法保持在原型中,从而让每个实例都继承这些属性或者方法。
最终方案
function tab(name,age){
this.age=age;
this.name=name;
}
tab.prototype={
content:function(){
return this.name;
},
init:function(){
return this.age;
}
}
var tab=new tab("haha","18")
console.log(tab.init())
console.log(tab.content())