(1) Object-oriented ---- inheritance

Source: Internet
Author: User

Tag: What is const? The ref setting failed to be understood quickly.

(1) Object-oriented ---- inheritance

It has been a long time since I came into contact with object-oriented users. I have been pondering this part of inheritance for a while, and I have not been able to collect various materials and websites to satisfy myself and quickly understand the inheritance knowledge, so I classified them and came up with my own summary.

Let's talk about inheritance first? Maybe what we first came into contact with about inheritance should be "Heritage "?? Maybe there are a lot of TV series, and there is always a family that will let you die for this stuff. You can't really watch it, but it's very kind and amiable for my big Js. After all, no one is competing, no accidents, no bugs, and so on. Don't talk nonsense. Go to the topic !!

1. Extend prototype Object Inheritance: Add new attributes to the prototype object.

This should be the most basic inheritance. By adding new attributes and methods to the object, the instance can be shared.

Example:

Function person (){};

Person. Prototype. Say = function (){};

VaR p1 = new person ();

==== In this case, P1 contains the say () method;

2. Replace prototype Object Inheritance: You need to add many methods to the instance. It is very troublesome to expand to the default prototype object one by one. In this case, we can replace the default prototype object, add them together.

Example:

Function bird (){};

Bird. Prototype. Say = function (){};

Bird. Prototype = {

Constructor: Bird,

C1: function (){},

C2: function (){},

C3: function (){}

}; // New prototype object

VaR maque = new bird ();

Console. Log (maque );

Console. Log (maque. constructor );

// Output through the console, you will find that the original say method does not exist, instead of the new C1, C2, and C3 Methods

  

3. Mixed Inheritance: copy the functions (attributes and methods) of an object to another object.

VaR o = {name: "Wang Wu", age: 30 };

VaR O2 = {Gender: "male "};

// Traverse the o Functions and add these functions to O2

For (var key in O ){

O2 [Key] = O [Key];

}

Console. Log (O2 );

4. Prototype + hybrid inheritance

Function man (){};

// Here, the preceding mixed operation is inherited and encapsulated into a function as follows:

Function extend (target, source ){

For (var key in source ){

VaR value = source [Key];

Target [Key] = value;

}

Return value;

}

// Extend multiple methods (same object) to man's prototype and call the above method.

Extend (man. prototype ,{

Age: function (){},

Height: function (){},

Nation: function (){}

});

VaR Hman = new man ();

Console. Log (Hman. Age ());

5. prototype chain inheritance: Create an object so that this object inherits from another object

// To create an object, a media (constructor) is required );

Function f (){};

VaR O2 = {age: 30 };

// Set the prototype object

F. Prototype = O2;

VaR O1 = new F ();

// Encapsulate the following:

Function create (O2 ){

Function f (){};

// Set the prototype object

F. Prototype = O2;

// Return the created object

Return new F ();

}

    

6. prototype chain inheritance: Any object inherits from the prototype object, and the prototype object also has its own prototype object. (Learn the prototype chain well)

7. Borrow constructor inheritance: You can use the apply () and call () Methods to call a super-Type constructor within a subclass constructor to execute constructor on the newly created object.

Function supertype (){

This. colors = {"red", "blue", "green "};

}

Function subtype (){

// Inherits the supertype

Supertype. Call (this );

}

VaR instance1 = new subtype ();

Instance1.colors. Push ("black ");

Console. Log (instance1.colors); // red, blue, green, black

 

VaR instance2 = new subtype ();

Console. Log (instance2.colors); // red, blue, green

 

 

Reprinted: http://www.cnblogs.com/Brookeshan/p/6091467.html

(1) Object-oriented ---- inheritance

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.