1、工厂模式创建对象
function 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(‘Qian‘,21,‘student‘);
var person2 = createPerson(‘Qian1‘,20,‘student‘);
2、构造函数创建对象
function Person(name,age,job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert(this.name);
};
}
var person1 = createPerson(‘Qian‘,21,‘student‘);
var person2 = createPerson(‘Qian1‘,20,‘student‘);
3、Object构造函数创建
var Person = new Object();
Person.name = ‘Qian‘;
Person.age = 21;