JavaScript NOTE 4: Object-oriented Programming

Source: Internet
Author: User

JavaScript NOTE 4: Object-oriented Programming

1. Understanding objects

Attribute type: Data Type and accessors type

Data Attribute: [[retriable]: whether the attribute can be deleted through delete to redefine the attribute or whether the attribute can be modified. Whether the attribute can be changed to the accessors attribute

[[Enumerable]: Can an attribute be returned through a for-in loop?

[[Writable]: whether the attribute value can be modified

[[Value]: data Value containing this attribute

Accessors attributes: [[retriable]: whether the attribute can be redefined by deleting the attribute through delete or whether the attribute attributes can be modified. Whether the attribute can be changed to a data attribute

[[Enumerable]: Can an attribute be returned through a for-in loop?

[[Get]: The function called when reading the attribute

[[Set]: The function called when writing an attribute

2. Create an object

Factory mode: process that abstracts specific objects. Using functions to encapsulate the details of creating objects with specific interfaces (solved the problem of creating objects multiple times, but did not solve the problem of Object Recognition)

Constructor mode: A constructor starts with an uppercase letter and does not explicitly create an object. It directly assigns attributes and methods to this object without ruturn objects. In the future, its instance can be identified as a specific type, which is exactly where the constructor mode is better than the factory mode.

Prototype mode: All created functions have a prototype attribute. This attribute is a pointer pointing to an object. This object contains attributes and methods that can be shared by all instances of a specific type. The advantage of using a prototype object is that all object instances can share its attributes and methods.

Constructor mode and prototype mode: constructor mode is used to define instance attributes, while prototype mode is used to define methods and shared attributes.

Dynamic Prototype mode: encapsulates all information in the constructor. By initializing the model in the constructor, the advantage of using both the constructor and prototype is maintained.

Parasitic constructor mode: Creates a function that encapsulates the code of an object and then returns the created object.

Safe constructor mode: similar to the mode of parasitic constructor, but the instance method of the newly created object does not reference this and the constructor is not called using the new operator.

3. Inheritance

Native chain: uses a prototype to inherit the attributes and methods of another reference type. Each constructor has a prototype object. The prototype object contains a pointer to the constructor, And the instance contains an internal pointer to the prototype.

The inheritance is implemented by creating a SuperType instance and assigning the instance to SubType. prototype.

Combination inheritance: combines the prototype chain with the technology group that borrow the constructor. Function reuse is realized by defining the method in the prototype, and each instance can have its own attributes.

Original Type inheritance: inheritance can be implemented without pre-defining constructors. The essence of inheritance is to execute a shortest copy of a given object.

Parasitic inheritance: similar to the original type inheritance, it creates an object based on an object or some information, enhances the object, and finally returns the object.

Parasitic combined inheritance: it combines the advantages of parasitic inheritance and combination inheritance, and is the most effective way to implement type-based inheritance.

 

 

 

Previous

Http://www.bkjia.com/kf/201412/359005.html

 

 

 

 

 

 

 

 

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.