Notes about creating objects in JavaScript

Source: Internet
Author: User

1, the two most basic ways to create objects: constructors | | Literal quantity

constructor function:

var New  = "Chen1zee1"= 18;

Literal:

var person = {name: "Chen1zee1", Age:18,}

    • Cons: When you create many objects using, constructors, and literals, you generate a lot of duplicate code. To solve this problem, people are starting to use Factory mode

2, Factory mode

Factory mode abstracts the process of creating concrete objects and encapsulates them into functions so that objects can be created as long as the corresponding function is executed. But because the class cannot be created in ECMAScript, the developer invented a function to encapsulate the details of creating objects with a particular interface. Cases:

function Createperson (name) {    varnew  Object ();     = name;     return o;} var person = Createperson ("Chen1zee1");

3, constructor mode

In ECMAScript, constructors can be used to create objects of a specific type, and you can also create custom constructors that define properties and methods for custom object types. Cases:

function Person (name) {    this. Name = name;}  var New Person ("chen1zee1");
    • The difference between a constructor and a factory pattern is that 1 does not explicitly create an object, 2 assigns properties and methods directly to the This object, and 3 does not need to return.
    • Also, by convention: Custom Constructors should start with a large letter to differentiate other functions.
    • Also note that you must use the new operator to create an object from a constructor function.

Problems with constructors: When you use constructors, each method is created once in each instance. For example:

function Person (name) {    this. Name = name;       This function () {        alert (this. Name);    }}

Whenever the bar uses this constructor, a method Sayname is created, and in fact, we do not need to deliberately set a Sayname method for each object, because the Sayname functionality of the different instances is the same.

4, prototype mode

    • When you create an instance of a constructor, you create an unnecessary method repeatedly. We have used the prototype (prototype) property of the function.
    • Each function we create has a prototype (prototype) attribute, which is a pointer to the prototype object of the function. This prototype object contains all the properties and methods of the constructor and is shared with the objects created by the constructor.

For example, the prototype object is like a recipe, and the constructor is the act of cooking, and the example is the dish we make by looking at the recipes.

The recipes (prototype objects) contain all the materials (example properties) required for cooking (the constructor), methods such as (frying and boiling), and so on, and all the materials that we have made out of the recipes, the methods (fried and boiled), can be found in the recipes.

Let's just say that the prototype object is a stem cell, and the example is the child cells that the stem cells split out of, the same thing. Of course, the child cells will have a mutation of the gene (the instance changes its own properties and methods, it will show its own properties and methods, when no definition and then cloned in the prototype.) It can also be understood that instances are examples of instances, and the instances do not look for their parents to find them, the expression of stem cells (prototypes) on the different side.

    • The advantage of using prototypes is that you can have all object instances share the properties and methods that it contains. That is, we do not have to define the object instance information in the constructor (that is, some functions of the function, such as the Sayname () above, can not be defined, let the instance be found from the prototype and called), but can be added to the prototype, for all instances to use. Thus optimizing the operation.

Cases:

function Person (name) {    = name;     function () {        alert (this. name);}    ;}

5. Understanding prototype Objects

As soon as we create a function, a prototype property is created for that object based on a specific set of rules. This property points to the function's prototype object, Fn Prototype.

The prototype object automatically obtains a constructor property, pointing to the constructor itself.

After the custom constructor has been created, its prototype object will only get the constructor property by default, while the other methods are inherited from object.

When the constructor is called to create a new instance, the inside of the instance contains a pointer to the __prototype__ (with compatibility issues), pointing to the constructor's prototype object.

Take person as an example to show the relationships between constructors, prototype objects, and instances:

Of course, because these several related attributes (prototype) are pointers, they can be modified to point to achieve a certain purpose.

    • The disadvantage of prototype mode: All attributes in the prototype are shared by many instances. And when there are properties of the reference type value in the prototype, for example: array []
    • Will cause all instances to have the same reference object, which we certainly do not want in most cases. For example:
function Person (name) {    = name;     = ["Dorlin", "Lily"];} var New Person ("chen1zee1");p Erson1.friends.push ("Jack"); var New Person ("Jack");p Erson2.friends.push ("chen1zee1");

As a result, the Person1 and Person2 friends are ["Dorlin", "Lily", "Jack", "Chen1zee1").

6, combining the constructor pattern with the prototype mode

We can take the advantages of both, the constructor defines the properties of the instance, let it have its own characteristics, and the prototype schema definition method has shared characteristics. For example, the Sayname () function, as well as when the person is class_1, can define Person.prototype.class = "Class_1";

function Person (name) {    this. Name =function() {alert (this = "Class _1 ";

    • The way that this constructor is mixed with prototypes is one of the most widely used and most recognized methods of creating custom types in ECMAScript. It can be said that this is a default pattern for defining reference types.

Notes about creating objects in JavaScript

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.