JS Create object

Source: Internet
Author: User

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

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.