Google closure--inheritance Module II: Goog.base () Demo analysis

Source: Internet
Author: User
Tags c constructor

Yesterday we talked about Goog.inherits (), which is responsible for completing inheritance by means of inheriting the prototype object of the parent constructor through the prototype chain for the child constructor prototype object. This inheritance only completes the inheritance of the prototype object, looking at the previous diagram:

Does it feel like the parent constructor is useless, remember the last article, the idea of building a Super object? This depends on another api,goog.base ().

Before looking at the source code let's take a look at a simple demo, review Goog.inherit (), and this demo will also help us understand what goog.base () can do.

Demo Code:

Output Result:

Analysis:

First put Goog.inherit () and Goog.base () aside, let's first analyze these three constructors: A,b,c.

A:

Constructor: Adds the name attribute to new (constructed) object and is named Jay Chou .

Prototype object: Adds the Details method, and the name of the object that called the method in the console output is the wife of Quincy .

(Do not rule out someone using call,apply and other methods to impersonate Jay Chou ...) )

B:

Constructor: Adds an age property to new (constructed) object and assigns a value of.

Prototype object: Add the Details method, and in the console output, the value of the name property of the object that called the method is a singer , age is the value of the object's property.

Here's a question: the object created by the constructor is not enough to execute its prototype object, in short, if we create an object with a B constructor, this object does not have a Name property, and if you do not add the Name property to the object later, the method of details is called. The Name property could not be found in the object, resulting in an error.

C:

Constructor: Adds a height property to new (constructed) object and copies it as "173cm".

Prototype object: The Details method, the output of the object that called the method, is Ye Huimei, and the Height property value of the object is tall.

Similar to B, the object constructed by the C constructor is also not sufficient to satisfy the details of its prototype object, because C constructs an object that does not have a name attribute .

Then let's analyze the results:

The person object is constructed from a constructor, but the following doubts exist:

1.Why does the person object have both the B constructor and the attributes added to the new object in the C constructor ?

2. Why does the details method of the person object not only execute the details method of the constructor a prototype object, but also execute the same method as the prototype object of B and C ?

This is the power of goog.base.

Goog.base () mainly has 2 kinds of use situation:

1. Use in constructors, such as:

First look at the role:

Locate the parent of a inherited constructor B, and then pass the current scope object to the B constructor and call it. This explains that the person object in question 1 has the age property constructed by the B constructor, and B has passed in a Scope object (person) in a and called its parent constructor C, so the person object has the height property.

2. Used in methods of constructor prototype objects, such as:

Here, the second parameter of Goog.base () is the method name of the prototype object that needs to call the prototype object constructor the constructor that points to the parent constructor , so this is a good explanation for the doubt 2, why the B prototype object was executed, Goog.base () is also called in B, so the details method of the C prototype object is executed.

In this chapter we mainly, did a simple demo, and analyzed the Goog.base () two main uses, and its effect, the following chapter we will focus on the analysis of Goog.base () source code.

Google closure--inheritance Module II: Goog.base () Demo analysis

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.