Seven ways JavaScript creates objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.