JavaScript Note Four: Object-oriented Programming

Source: Internet
Author: User
Tags shallow copy uppercase letter

1. Understanding the Object

Property type: Data type and accessor type

Data properties: [[[Configurable]]: can delete properties by Delete to redefine properties or can modify properties of attributes, can you modify properties to accessor properties

[[[Enumerable]]: Ability to return properties via For-in loop

[[writable]]: Ability to modify the value of a property

[[Value]]: Contains data values for this property

Accessor properties: [[[Configurable]]: Can I delete a property by deleting it to redefine the attribute or can I modify the attribute of the property to modify the property to a data property

[[[Enumerable]]: Ability to return properties via For-in loop

[[Get]]: The function that is called when the property is read

[[Set]: The function that is called when the property is written

2. Create objects

Factory mode: The process of abstracting concrete objects. Use a function to encapsulate the details of creating an object with a specific interface (resolves the problem of creating objects multiple times, but does not solve the problem of object recognition)

Constructor mode: The constructor begins with an uppercase letter it does not explicitly create an object, assigns properties and methods directly to the This object, and does not have a Ruturn object. It is possible to identify its instance as a specific type in the future, which is where the constructor pattern trumps the factory pattern.

Prototype mode: The created function has a prototype property, which is a pointer to an object that contains properties and methods that can be shared by all instances of a particular type. The advantage of using a prototype object is that you can have all of the object instances share the properties and methods that it contains.

The combination uses the constructor pattern and the prototype pattern: The construction mode is used to define the instance properties, and the prototype pattern is used to define the methods and shared properties.

Dynamic prototype mode: Encapsulates all information in a constructor, while preserving the advantages of using constructors and prototypes by initializing the model in the constructor

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

Secure constructor Mode: follows a pattern similar to the parasitic constructor, but the instance method of the newly created object does not reference this and does not call the constructor with the new operator.

3. Inheritance

Native chain: Use the prototype to have one reference type inherit the properties and methods of another reference type. Each constructor has a prototype object, and the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype.

Inheritance is accomplished by creating an instance of supertype and assigning that instance to Subtype.prototype.

Combinatorial inheritance: Combine the prototype chain and the technology that borrows the constructor into one piece. The function is reused both by defining the method on the prototype and ensuring that each instance has its own properties.

Prototype inheritance: You can implement inheritance without having to pre-define constructors, essentially by performing a shallow copy of a given object.

Parasitic inheritance: Very similar to prototype inheritance, creating an object based on an object or some information, then enhancing the object, and finally returning the object.

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

JavaScript Note Four: Object-oriented Programming

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.