Introduction to several methods for creating objects using javascript _ basic knowledge-js tutorial

Source: Internet
Author: User
The following section introduces several methods for creating javascript objects. I think this is quite good. Now I will share it with you and give you a reference to the attributes and methods contained in object operations that can be created in several js modes.

Generally, the first letter of the constructor name is an upper-case letter, and the first letter of the non-constructor name is a lower-case letter. Of course, the only difference between a constructor and a common function is that the calling method is different. Therefore, if a function is called through new, it can be used as a constructor. If it is not called through new, it is the same as a general function.

Let's talk about my understanding of these modes:

Factory mode: create a general function, create an Object in the function, add attributes and methods to the Object, grant the value to the Object, and finally return the Object. The object type cannot be recognized.

Constructor mode:Create a constructor and use this to assign values. Each time an instance is created, the method is created once, and each method executes the same command, which is redundant. This disadvantage can be solved by putting the method in the global environment, but there is no encapsulation. However, it can be solved through the prototype mode.

Prototype:Each function has a prototype attribute, which is a pointer to an object that contains the attributes and Methods shared by all instances created by the function.

The relationship between the prototype object, constructor, and instance is as follows:

Illustration: 1: constructor and instances created by constructor. Their prototype attributes all point to the prototype object of the constructor.

2: The prototype object of the constructor has the constructor attribute pointing to the constructor.

3: All attributes and methods contained in the prototype object of the constructor can be shared by all instances created by the constructor.

After the prototype object is rewritten using the object literal, the constructor points to the object constructor. to point to another constructor, You need to modify the value of the constructor attribute of the prototype object, for example: constructor: in this way, even if the Person prototype object is overwritten, the constructor of the prototype object still points to the Person constructor.

When creating an instance first: If you directly add attributes or methods, the instance can be accessed.

If the prototype object is rewritten, the prototype of the constructor points to the new prototype object, while the prototype of the previously created instance still points to the original prototype object, therefore, the instance cannot access the new attributes or methods of the new prototype object.

The prototype object contains shared attributes and methods, so each instance has this information, so there is no difference between instances, and parameters cannot be passed, this is not what we want. Each instance has common information and different information, so we can combine the constructor mode and the prototype mode.

The combination of constructor mode and prototype mode:

Mode prototype mode: combines an independent constructor with its prototype object, initializes the prototype in the constructor, and adds a method to it.

If the method does not exist, it is added to the prototype object and executed only once when the prototype is initialized.

Parasitic constructor mode: similar to the factory mode, the difference is that the parasitic constructor mode is constructor and instances are created through new.

Safe constructor mode: The method does not reference this object because there is no public attribute. New is not used when creating an instance. You can only access attributes (that is, imported data) through methods ).

The introduction of the above javascript Object creation modes is all the content shared by the editor. I hope to give you a reference and support for the script.

Related Article

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.