JS Create object Several different methods of detailed _javascript skills

Source: Internet
Author: User
Tags hasownproperty

This article introduced several JS to create the object the method, shares for everybody to consult, the concrete content is as follows

1. Factory mode

Disadvantage: There is no solution to the problem of object identification, that is, how to know the type of an object.

2, the constructor function pattern


compared with Factory mode:
1, there is no explicit creation of an object
2, directly to the properties and methods assigned to 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 it actually goes through 4 steps:
1. Create a new object
2. Assigning the scope of a constructor to a new object
3, executing the code in the constructor
4. Return new Object
Creating a custom constructor can identify its instance as a specific type.
Disadvantages of Constructors:
Each method has been recreated on each instance. Both Person1 and Person2 have a sayname () method, but two methods are not the same function instance. Functions with the same name on different instances are not equal.
It is not necessary to create two function instances to complete the same task, and the This object does not need to bind the function to a specific object before executing the code, as follows.

The Sayname property is set to the global sayname function, so that because Sayname contains a pointer to a function, the Person1 and Person2 objects share the same function.
However, if an object needs to define many methods, there are many global functions to be defined, and a custom reference type is not encapsulated. In order to solve these problems, the prototype model is introduced.

3. Prototype mode

Understanding the Prototype object
Each function we create has a prototype attribute, which is a pointer to an object that is intended to contain properties and methods that can be shared by all instances of a particular type. Prototype is the object prototype of the object instance that was created by calling the constructor, and the advantage of using a prototype object is that all object instances share the properties and methods it contains.


First, the parser asks whether the instance Person1 has a name attribute and, if so, returns.
If not, continue to search the Person1 's prototype for the name attribute, and if so, return.
If not, continue to search the prototype of the Person1 prototype.

isprototypeof () determines 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 of person.
Console.log (object.getprototypeof (person1) = = Person.prototype)//true
Console.log (Object.getprototypeof (person1). Name);/"Yvette"
The hasOwnProperty () method can detect whether a property exists in the instance or in a prototype, and returns true only if the given property exists in the instance.
Console.log (Person1.hasownproperty ("name"));//false
prototypes and in Operators
There are two ways to use the In operator: separate use and use in the for-in loop. When used alone, the in operator returns true if the given property is accessible through an object, whether the property is in the instance or in the prototype.
Using the For In loop returns all enumerable properties that can be accessed through an object, including both the properties in the instance and the properties that exist in the prototype. If a property in the instance masks a property that is not enumerable in the prototype, it is also returned. There is a bug in the previous version implementation of IE9, and instance properties that mask the enumerable properties are not returned in for-in.


There is no log information in the IE9 before the bar. Although the ToString () method in the person instance masks an enumerable ToString () in the prototype;
prototype Shorthand


This causes the person1.constructor to point to the object instead of to the person. If constructor is important, you need to deliberately set it to the appropriate value, such as:


However, this approach causes the constructor property to become enumerable.
If you want to be set as not enumerable (default is not enumerated), you can use Object.defineproperty (Person.prototype, "constructor", {
Enumerable:false,
Value:person
});
The dynamic nature of the prototype
Because the process of looking up a value in a prototype is a search, any changes we make to the prototype object are immediately reflected from the instance.
If you rewrite the entire prototype object, the situation is different. Calling a constructor adds a [[prototype]] pointer to the original prototype for the instance, and modifying the prototype to another object is tantamount to cutting off the relationship between the constructor and the original prototype. The pointer in the instance points only to the stereotype, not to the constructor.


The Person.prototype points to the original prototype object and does not point to the new prototype object.
problem with a prototype object
The biggest problem with archetypal patterns is that they are shared by nature.
The problem is more pronounced for properties that contain reference type values


The intention is only to modify the Person1 friends, but it causes Person2 friends attribute values to change. So we rarely use the prototype model alone.

4, combined use of construction patterns and prototype mode

The most common way to create custom types is to combine the constructor pattern with the prototype pattern. Constructor patterns are used to define instance properties, which are used to define methods and shared properties, so that each instance has a copy of its own instance attribute, while also sharing a reference to the method, saving maximum memory.

In addition to the above several ways, there are dynamic prototype mode, parasitic structural patterns and sound construction patterns, but in view of the low frequency of use, no longer repeat.

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.