JavaScript advanced Programming--three ways to create objects

Source: Internet
Author: User

Three ways to create objects:

1. Factory mode

Factory mode is a well-known design pattern in the field of software engineering, which abstracts the process of creating concrete objects. The following is an example of creating an object using the factory function.

2. Constructors:

From the example above, we see the difference between a constructor and a factory function:

1. There is no explicit creation of an object

2. Assign properties and methods directly to this object, no return statement

In addition, the function name person uses the first letter capitalized. (This is a convention in which constructors should always start with an uppercase letter, and not a constructor should start with a lowercase letter.) )

Using a constructor to create an object means that you can get the type of the object. This is where the constructor is better than the factory pattern. Detects object types, commonly used instanceof operators.

3. Prototype Inheritance:

The two objects we created using the constructor, Person1 and Person2, are both instances of the person object and an instance of the object. This is because all objects are indirectly or directly inherited from object. The main problem with constructors is that each method is recreated on each instance, which is not necessary. This problem is solved by transferring the definition of the method to the outside of the constructor. You can solve this problem by using prototype mode.

Each function you create has a prototype (prototype) attribute, which is a pointer to an object, the prototype object. The advantage of using a prototype object is that you can have all object instances share the properties and methods that it contains.

As you can see, Person1 and Person2 share the properties and methods of their prototype objects. In other words, Person1 and Person2 access the same set of properties and the same method.

The above Person1 and person2 examples are called Jason, we intended Person1 called Jason,person2 called Poppy, how to do?

As we can see from the above example, Person2 's information has been changed and is what we want. Why? When you add a property to an object instance, this property masks the property of the same name saved in the prototype object; Notice that it is a mask, and the property with the same name in the prototype object has not changed.

In the previous example, each time you add a property and a method, you will have to knock Person.prototype. A better approach is to rewrite the object's prototype with an object literal that contains all the properties and methods, as shown below. It is important to note that the object instances that were rewritten before and after the rewrite are actually pointing to different object prototypes.

An extra Constructor:person is included because objects created by literal form automatically get the constructor property, which does not point to the person function.

JavaScript advanced Programming--three ways to create objects

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.