1.直接创建对象
var shuaige={
name:"帅哥",
age:23,
height:183,
showInfo:function(){
console.log(this.name)
}
}
var meinv={
name:"美女",
age:22,
height:170,
showInfo:function(){
console.log(this.name)
}
}
优点:简单
缺点:无法进行相同对象量产
2.工厂模式
function createGongren(name,age,height,gonglin){
var obj={}
obj.name=name
obj.age=age
obj.height=height
obj.gonglin=gonglin
obj.showInfo=function(){
console.log(this.name)
}
return obj
}
var g1=createGongren("赵大",51,170,31)
var g2=createGongren("钱二",42,175,10)
var g3=createGongren("孙三",35,185,5)
有点:快速进行想同类型对象的量产
缺点:无法明确,确定的类型
3.构造函数
function Nongmin(name,age,height,tudi){
this.name=name
this.age=age
this.height=height
this.tudu=tudi
this.showInfo=function(){
console.log(this.name)
console.log(this.tudi)
}
}
var n1=new Nongmin("赵大",36,178,5)
var n2=new Nongmin("钱二",45,167,8)
优点:量产、又能检测对应的类型
缺点:相同的方法,没有开辟共同的空间,导致内存消耗
4.原型创建对象
function Shibing(name,age,height){
this.name=name
this.age=age
this.height=height
}
Shibing.prototype.showInfo=function(){
console.log(this.name)
}
var s1=new Shibing("赵大",21,178)
var s2=new Shibing("钱二",20,173)
var s3=new Shibing("孙三",22,176)
var s4=new Shibing("李四",20,174)