
1、构造函数模式,没有显示创建对象,直接将属性方法赋给this,没有return语句。
每个方法都要在每个实例上重新定义一遍,无法得到复用。
functionPerson(name,age){
this.name=name;
this.age=age;
this.sayName=function(){
console.log(this.name)
}
}
varperson1=newPerson('chen',21)
2、混合构造函数原型模式看,构造函数模式用于定义实例属性,原型模式用于定义方法和共享的属性。
functionPerson(name,age){
this.name=name;
this.age=age;
}
Person.prototype={
constructor:Person,
sayName:function(){
console.log(this.name)
}
}
varperson1=newPerson('chen',21)
以上就是javascript创建对象的方法,希望对大家有所帮助。更多Javascript学习指路:Javascript
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















































暂无评论内容