First, the factory model
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 (' Nicholas ', $, ' software Engineer ');
var person2=createperson (' Greg ', ' Doctor ');
Factory mode solves the problem of creating multiple objects, but does not solve the problem of object recognition (that is, how to know the type of an object).
Second, the structural function mode
function Person (name,age,job) {
This.name=name;
This.age=age;
This.job=job;
This.sayname=function () {
alert (this.name);
};
}
var person1=new person (' Nicholas ', $, ' software Engineer ');
var person2=new person (' Greg ', ' Doctor ');
Compared to the factory model:
1. Create objects without display
2. Assign properties and methods directly to the This object
3. No Return statement
JS Create object