JavaScript Advanced Programming (3rd Edition) Chapter Sixth reading notes

Source: Internet
Author: User
Tags instance method uppercase letter hasownproperty

The sixth chapter object-oriented programming

1. Data properties

[[Configurable]]: Indicates whether the property can be redefined by deleting the property from the delete. The default value is true.

[[Enumerable]]: Indicates whether the property can be returned through a for-in loop. The default value is true.

[[writable]]: Indicates whether the value of the property can be modified by default to TRUE.

[[Value]]: Contains the data value for this property. The default is undefined.

To modify the property's default attributes, you must use the ECMAScript5 Object.defineproperty () method. Receives three parameters: the object where the property is located, the property name and a descriptor object. Where the property of the Descriptor object must be: Configurable, enumerable, writable, and value.

The Object.defineproperty () method can be called multiple times to change the same property, but there is a limit after setting the configurable attribute to false.

2. Accessor Properties

Accessor properties do not contain data values and contain a pair of getter and setter functions.

[[Configurable]]: Indicates whether the property can be redefined by deleting the property from the delete. The default value is true.

[[Enumerable]]: Indicates whether the property can be returned through a for-in loop. The default value is true.

[[Get]]: The function that is called when the property is read, the default value is undefined.

[[Set]]: The function that is called when the property is written, the default value is undefined.

Defining multiple properties, ECMASCRIPT5 defines the Object.defineproperties () method.

3. Using the ECMAScript5 Object.getownpropertydescriptor () method, you can obtain a descriptor for a given property. This method receives two parameters: the object where the property resides and the name of the property whose descriptor you want to read. The return value is an object.

4. Create an Object

Method

Details

Factory mode

Use functions to encapsulate the details of creating objects with specific interfaces.

Pros: You can call functions countless times.

Cons: But there is no problem solving object recognition.

Constructor mode

Create object without display;

The properties and methods are assigned directly to the This object;

There is no return statement.

Constructors always start with an uppercase letter, and non-constructors should start with a lowercase letter.

To create a new instance of a constructor, you must use the new operator.

Advantage: The constructor pattern is better than the factory pattern in that it is possible to identify its instance as a specific type.

Cons: Each method is recreated on each instance.

Prototype mode

Each function has a prototype (prototype) property, 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.

When you add a property to an object instance, this property masks the property of the same name saved in the prototype object, but does not modify that property. Using the delete operator, you can completely remove the instance properties and regain access to the properties in the prototype.

The hasOwnProperty () method can detect whether a property exists in an instance or exists in a prototype. Returns true only if it exists in the object instance.

When used alone, the in operator returns True when the given property is accessible through the object, whether the attribute exists in an instance or in a prototype. As long as the in operator returns True, and hasOwnProperty () returns False, the properties in the prototype can be determined.

The Hasprototypeproperty () method returns True when the prototype attribute is present, and returns False when the prototype property is overridden by the instance.

When you use the for-in loop, you return all the enumerable properties that can be accessed through the object, including the attributes that exist in the instance, as well as the properties that exist in the prototype.

Overriding the prototype object cuts the connection between the existing prototype and any previously existing object instances, and they are still referencing the original prototype.

Pros: You can have all object instances share the properties and methods that it contains, that is, you do not have to construct the information that defines the object instance in the function, but you can add that information directly to the prototype object.

Disadvantage: By sharing the essence, the problem is prominent for attributes that contain reference type values.

Combining the constructor pattern with the prototype pattern

The most common way to create customizations is to define the default form of reference types.

The instance properties are defined in the constructor, and the properties constructor and methods shared by all instances are defined in the prototype.

Advantages: The set constructor and prototype model of the director.

Dynamic Prototyping Mode

Use the IF statement to check for any properties or methods that should exist after initialization. An object created with this pattern can use the instanceof operator to determine its type.

Parasitic constructor mode

This pattern is the same as the factory pattern, except that the new operator is used and the wrapper function used is called a constructor. Instead of returning a value, the constructor returns the new object instance by default, and by adding a return statement at the end of the constructor, you can override the value returned by the calling constructor.

The returned object has no direct relation to the constructor or the stereotype property of the constructor.

You cannot rely on the instanceof operator to determine the object type.

You can use other modes and try not to use this mode

Secure constructor Mode

Secure object: There is no public property, and its method does not refer to the object of this.

differs from the two point of the parasitic construction pattern: First, the instance method of the newly created object does not reference this, and the constructor is not called with the new operator.

5. The OO language generally supports two types of inheritance: interface integration and implementation inheritance. Interface integration inherits only the method signature, and the implementation inherits the actual method of the set. ECMAScript only supports implementation of inheritance, and relies primarily on the prototype chain to implement.

6. The basic idea of a prototype chain is to have a reference type inherit the properties and methods of another reference type using the prototype. 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 object.

Functionsupertype () {

This.property = true;

}

supertype.prototype.getsupervalue= function () {

Returnthis.property;

}

Functionsubtype () {

This.subproperty = false;

}

Subtype.prototype= new Supertype ();

subtype.prototype.getsubvalue= function () {

return this.subproperty;

}

Instance.getsubvalue ()//false

Instance.getsupervalue ()//true

7. All reference types are integrated with object by default, and this inheritance is implemented through the prototype chain.

8. Two methods for determining prototypes and instances: One is to use the instanceof operator to test the constructor in the instance and prototype chain, and the result returns True, and the second is to use the isprototypeof () method, as long as the prototype is present in the prototype chain. Can be said to be the prototype of an instance derived from the prototype chain.

9. The code that adds a method to the prototype must be placed after the statement that replaced the prototype, and in addition, the object literal cannot be used to create the prototype method when inheriting through the prototype chain.

10. Inheritance

Method

Realize

Prototype 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 object.

One of the two major problems with the prototype chain is that it comes from a prototype that contains a reference type value, and the other is that when you create an instance of a subtype, you cannot pass parameters to the super-type constructor.

Borrowing constructors

Use the Apply () and call () methods to execute the constructor on the newly created object.

Pros: You can pass parameters to a superclass constructor in a subtype constructor relative to the prototype chain

Cons: methods are defined in constructors, so function reuse is not a problem.

Portfolio Integration

Combine the prototype chain and the technology that borrows the constructor to take advantage of the way. The principle is to use the prototype chain to achieve the integration of prototype properties and methods, and by borrowing constructors to implement inheritance of instance properties.

Advantages: Avoid the defects of prototype chain and borrowing constructors, and combine their advantages to become the most common inheritance pattern in JavaScript. Furthermore, instanceof and isprototypeof () can also be used to identify objects that are created based on composite inheritance.

Disadvantage: In any case, the two-time superclass constructor is called, one time when the prototype of the subtype is created, and the other is inside the subtype constructor.

Prototype inheritance

This method does not have a strict constructor, and the prototype allows you to create a new object based on an existing object without having to create a custom type.

ECMAScript normalized the stereotype inheritance by adding the Object.create () method.

Disadvantage: Properties that contain reference type values will always share the corresponding values, just as you would with a prototype pattern.

Parasitic inheritance

Create a function that encapsulates the inheritance process, which internally enhances the object in some way, and then returns the object as if it were processed.

Cons: Using parasitic integrations to add functions to objects can reduce efficiency due to the inability to reuse functions

Parasitic combined inheritance

By borrowing constructors to inherit attributes, the method is inherited through the compositing form of the prototype chain. The basic idea is that you don't have to call a super-strong constructor to specify a prototype of a subtype, we need a copy of the super-type prototype. Even if you inherit a super-type prototype with parasitic inheritance, the result is then assigned to the prototype of the subtype.

Advantages: High efficiency, the value of the call to a super-type prototype of the constructor, the prototype chain can also remain unchanged, can normally use instanceof and isprototypeof (). Is the most ideal inheritance paradigm.

JavaScript Advanced Programming (3rd Edition) Chapter Sixth reading notes

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.