First, the factory model
Create:
function Createperson (name,behavior) {
var p=new Object ();
P.name=name;
P.behavior=behavior;
P.getinfo=function () {
Alert (this.name+ "in" +this.behavior)
}
P.getinfo ();
}
var Person=createperson ("Zhang San", ["Play Game", "read book"]);
Second, the structural function mode
Create:
function Createperson (name,behavior) {
This.name=name;
This.behavior=behavior;
This.getinfo=function () {
Alert (this.name+ "in" +this.behavior)
}
}
var person=new createperson ("Zhang San", ["Play Game", "read book"]);
Person.getinfo ();
Third, prototype mode
Create:
function Createperson () {}
Createperson.prototype.name= "Zhang San";
createperson.prototype.behavior=["Playing games", "reading"];
Createperson.prototype.getinfo=function () {
Alert (this.name+ "in" +this.behavior)
}
var person=new createperson ();
Person.getinfo ();
Iv. combination modes (constructors and prototypes)
Create:
function Createperson (name,behavior) {
This.name=name;
This.behavior=behavior;
}
Createperson.prototype.getinfo=function () {
Alert (this.name+ "in" +this.behavior)
}
var person=new createperson ("Zhang San", ["Play Game", "read book"]);
Person.getinfo ();
Five, dynamic prototype mode
Create:
function Createperson (name,behavior) {
This.name=name;
This.behavior=behavior;
if (typeof this.getinfo!= "function") {
Createperson.prototype.getinfo=function () {
Alert (this.name+ "in" +this.behavior);
}
}
}
var person=new createperson ("Zhang San", ["Play Game", "read book"]);
Person.getinfo ();
Vi. Parasitic structural function patterns
Create:
function Createperson (name,behavior) {
var p={};
P.name=name;
P.behavior=behavior;
P.getinfo=function () {
Alert (this.name+ "in" +this.behavior)
}
P.getinfo ();
}
var person=new createperson ("Zhang San", ["Play Game", "read book"]);
Seven, the SAFE structure function pattern
Create:
function Createperson (name,behavior) {
var p={};
P.getinfo=function () {
Alert (name+ "in" +behavior)
}
P.getinfo ();
}
var person=new createperson ("Zhang San", ["Play Game", "read book"]);
Reference URL: http://www.cnblogs.com/ifat3/p/7429064.html
Seven ways JavaScript creates objects