JS Create object Several different methods of specific explanation

Source: Internet
Author: User
Tags hasownproperty

1. Factory mode

Cons: There is no problem solving object recognition, that is, how to know the type of an object.

2. Constructor mode


Compared to the factory model:
1. There is no explicit creation of an object
2. Assign properties and methods directly to the This object
3. No Return statement
To create an instance of person, you must use the new operator to call the constructor in such a way that you actually go through 4 steps:
1. Create a new object
2. Assigning the scope of a constructor to a new object
3. Run the code in the constructor
4. Return to new Object
Creating a constructor of its own defines the ability to identify its instance as a specific type.
disadvantages of the constructor:
Each method is created again and again on each instance. Both Person1 and Person2 have a method of Sayname (). However, two methods are not the same instance of a function. Functions with the same name on different instances are not equal.


It is not necessary to create two function instances that have completed the same task, and the This object is not required to bind the function to a specific object before running the code, as follows.

Set the Sayname property to the global sayname function, so that sayname includes a pointer to a function, so the Person1 and Person2 objects share the same function.
However, assuming that the object needs to define very many methods, it is necessary to define very many global functions, and the reference types that you define are not encapsulated.

In order to solve the above problems, the prototype model is introduced.

3. Prototype mode

Understanding prototype Objects
Each function we create has a prototype property, which is a pointer. Points to an object that is used to include properties and methods that can be shared by all instances of a particular type. Prototype is the object prototype of the object instance created by invoking the constructor, and the advantage of using a prototype object is that it allows all object instances to share the properties and methods it includes.



First, the parser will ask if the instance Person1 has a name attribute. Suppose there is. will return.
Assuming no, proceed to the Person1 prototype to search for the name attribute. Return if you have one.
If not, continue to search for prototypes of Person1 prototypes.



isprototypeof () determining the association between an instance and a prototype object
Console.log (Person.prototype.isPrototypeOf (Person1)); True
object.getprototypeof () returns the value of [[prototype]]
Console.log (object.getprototypeof (Person1));
person {name: "Yvette", age:26, Job: "Engineer"} returns the prototype object for person.


Console.log (object.getprototypeof (person1) = = = Person.prototype)//true
Console.log (Object.getprototypeof (person1). name);//"Yvette"
The hasOwnProperty () method can detect that a property is present in the instance. Still exists in the prototype, only a given attribute exists in the instance. Will not return true.


Console.log (Person1.hasownproperty ("name"));//false
prototypes and in Operators
There are two ways to use the In operator: use separately and in the for-in loop.

When used alone, the in operator returns True when the object is able to access the given property, whether the property is in an instance or in a prototype.
Using the For In loop, returns all the enumerable properties that can be interviewed by the object, including the attributes in the instance. Also includes attributes that exist in the prototype. It is also returned if the attributes in the instance are shielded from the non-enumerable properties in the prototype. There is a bug in the implementation of the version number prior to IE9, and an instance property that masks the non-enumerable property is not returned in for-in.

Before IE9, there is no log information in this section. Although the ToString () method in the person instance masks the non-enumerable ToString () in the prototype;
prototype Shorthand

This causes the Person1.constructor to no longer point to person, but to object.

Assuming constructor is very important, you need to deliberately set it to the appropriate value, such as:

This way, however, causes the constructor property to become enumerable.
Suppose you want to set to non-enumerable (the default is not enumerable), you can use Object.defineproperty (Person.prototype, "constructor", {
Enumerable:false,
Value:person
});
The dynamic nature of prototypes
Because the process of finding a value in a prototype is a search. As a result, whatever changes we make to the prototype object can be reflected immediately from the instance.
Assuming that the entire prototype object is rewritten, the situation is different. When the constructor is called, a [[prototype]] pointer to the original prototype is added to the instance, and altering the prototype to another object is tantamount to cutting off the connection between the constructor and the original prototype. The pointer in the instance only points to the prototype, not to the constructor.

Person.prototype points to the original prototype object, not to the new prototype object.
problems with prototype objects
The biggest problem with prototype mode is caused by the nature of its sharing.
For attributes that include reference type values, the problem is more prominent

Originally intended only to change Person1 friends, but led to Person2 friends attribute value also changed. So we rarely use prototype mode alone.

4. Combination of construction mode and prototype mode

The most frequently used way to create your own definition types is to combine the constructor pattern with the prototype pattern.

The constructor pattern is used to define instance properties, which are used to define methods and shared properties, so that each instance has its own copy of the instance properties, and at the same time shares a reference to the method, saving memory to a maximum.

In addition to the above methods, there are also dynamic prototype mode, parasitic tectonic mode and the safe structure mode, but given the low frequency of use, no longer repeat.

JS Create object Several different methods of specific explanation

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.