JS How to create a custom object

Source: Internet
Author: User

Factory mode

Pros: Accept parameters, you can call this function countless times, create a person object, and each time he can return a method containing three properties of the object.

Disadvantage: Although the problem of creating multiple similar objects is resolved, there is no problem with object recognition (that is, how to know the type of an object).

Constructor mode

JS allows you to create custom constructors that define custom object properties and methods.

The code in person () differs from the Createperson () :

1. Do not explicitly create an object

2. Assign properties and methods directly to the This object

3. No Return statement

to create a new instance of the person object, you must use the operator new. Calling a constructor in this way century will go through four steps:

1. Create a new object.

2. Assign the scope of the constructor to the new object (so this will point to the new object)

3. Execute the code in the constructor (add attributes for this new object)

4. Return to new Object

Pros: Creating a custom constructor means that its instance can be identified as a specific type in the future.

Cons: Each method should be re-innovated on each instance. Person1,person2 has a method named Sayname (), but these two methods are not instances of the same function. Because a function in ECMAScript is an object, every definition of a function is an instantiation of an object.

Prototype mode

? Understanding prototype Objects

1. Each function we create has a prototype (prototype) attribute, which is a pointer to the prototype object of the function.

2. By default, all prototype objects automatically get a constructor property, which is a pointer to the function where the prototype property is located.

3. When the constructor is called to create a new instance, the inside of the instance contains a pointer (internal property) [[[Prototype]], which points to the prototype object of the constructor.

Attention:

1. Each instance of a person contains an internal property that points only to someone. Prototype; in other words, there is no direct relationship between him and the constructor function.

2. Although none of the two instances contain properties and methods, we can call Person1.sayname (). This is accomplished by looking up the properties of the object.

When the code reads the properties of an object, the search is performed once, and the target is a property with the given name. The search starts first from the object instance itself, if a property with the given name is found in the instance, returns the value of the property, and if it is not found, continues the search for the prototype object pointed to by the pointer, looking for the property with the given name in the prototype object.

Pros: Reduce duplication of code and create objects using identities.

Cons: 1, it omits the process of passing initialization parameters to the constructor, and the result is that all forces will get the same attribute value by default.

2. All the attributes in the prototype are shared by many forces, and this kind of sharing works well for functions, but it makes sense for attributes that contain basic values, but it is a problem for attributes that contain reference type values, because instances generally have all of their own properties.

Combining the constructor pattern with the prototype pattern

The constructor pattern is used to define instance properties, and the prototype schema is used to define methods and shared properties. In this way, each instance has its own copy of the strength attribute, but at the same time it shares a reference to the method, maximizing memory savings. It also supports passing parameters to the constructor. This approach is also one of the most widely used and most recognized ways to create custom types in ECMAScript.

In addition, there are dynamic prototypes, parasitic structural function patterns, and secure structural function patterns.

JS How to create a custom 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.