javascript创建对象
1. 工厂模式
函数内创建Object,并通过函数传入参数构建各属性,最后返回该Object.1
2
3
4
5
6
7
8
9
10
11
12function createPerson(name, age, job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alert(this.name);
};
return o;
}
var person1 = createPerson("Nicholas", 29, "Software Engineer");
var person2 = createPerson("Greg", 27, "Doctor");
2. 构造函数模式
1 | function createPerson2(name,age,job) |