Common methods and comparisons for creating custom objects in JS

Source: Internet
Author: User

I am a beginner in JavaScript. I am reading the "JavaScript advanced programming" by Nicholas C. Zakas. I have seen the creation object and briefly summarized the content in the book!

Copy to ClipboardReference: [www.bkjia.com] <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
</Head>
<Body>
<Script type = "text/javascript">
// 1. Create an Object and add an attribute to it
Var person = new Object ();
Person. name = "Nicolas ";
Person. age = 29;
Personal. job = "Software Engineer ";

Personal. sayName = function (){
Alert (this. name );
};

/*
Disadvantage: using an interface to create many objects will produce a large number of repeated code
*/

// 2. Factory Mode
Function createPersonal (name, age, job ){
Var o = new Object ();
O. name = name;
O. age = age;
O. job = job;
O. sayName = function (){
Alert (this. name );
};
Return o;
}

/*
The factory mode solves the problem of object similarity, but does not solve the problem of Object Recognition (how to know the type of an object)
*/

// 3. constructor Mode
Function Person (name, age, job ){
This. name = name;
This. age = age;
This. job = job;
This. sayName = function (){
Alert (this. name );
};
}

/*
In the constructor mode, no explicit object is created. Methods and attributes are directly assigned to this object without a return statement.
To create a new instance of Person, you must use the new operator. Actually, there are four steps.
A. Create a new object
B. Assign the constructor scope to a new object (this points to this new object)
C. Execute the constructor to add attributes for the new object.
D. Return the new object.
Objects Created in constructor mode can recognize their types, but each method must be re-built on each instance.
*/

// Create two instances. It is unnecessary to create two identical methods. You can convert the function definition to the external method.
Function Person (name, age, job ){
This. name = name;
This. age = age;
This. job = job;
This. sayName = sayName;
}

Function sayName (){
Alert (this. name );
}

/*
This solves the problem of Two Methods doing the same thing, but the function defined in the global scope can only be called by an object. If the object definition requires many parties, many global functions must be defined, no encapsulation is available for custom reference types.
*/

// 4. prototype mode
Function Person (){
}

Person. prototype. name = "Nocholas ";
Person. prototype. age = 29;
Person. prototype. job = "Software Engineer ";
Person. prototype. sayName = function (){
Alert (this. name );
}

/*
All parameters in prototype mode get the same value by default. If the attribute is of the reference type, multiple instances will affect each other.
*/

// 5. constructor prototype hybrid mode
Function Person (name, age, job ){
This. name = name;
This. age = age;
This. job = job;
}

Person. prototype. sayName = function (){
Alert (this. name );
}

/*
Constructor is used to define instance attributes. prototype is used to define methods and shared attributes ,. Eigenvalue instances all have copies of their instance attributes,

At the same time, you can use the reference to the shared method. Currently, it is almost the most suitable object creation method */

</Script>
</Body>
</Html>

Related Article

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.