js创建对象的5种方式

1、创建简单对象:使用对象字面量{}创建对象。

2、用function(函数)模拟class(无参构造函数)。

3、使用工厂创建(Object关键字)。

4、prototype关键字采用原型对象。

5、混合模式(原型和构造函数)。

实例

functionPerson(name,age,job){
this.name=name;
this.age=age;
this.job=job;
this.sayname=function(){
alert(this.name);
};
}
varperson1=newPerson("a",29,"cc");
varperson2=newPerson("b",26,"cc");
原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享