Inheritance example for Javascript programming _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the inheritance Implementation Method of Javascript programming, and summarizes five common inheritance Techniques Based on instance form analysis, which has some reference value, if you need it, refer to the examples in this article to illustrate the inheritance of Javascript programming. We will share this with you for your reference. The details are as follows:

This article is a summary after reading "Return of the Javascript King". The detailed chapter of this article is in chapter 21st, P537.

Inheritance generally has the following three meanings:

1) subclass instances can share methods of parent classes;
2) subclass can overwrite the parent class method or extend the new method;
3) subclass and parent class are both of the subclass instance types.

1. Construct the Inheritance Law

The subclass calls the constructor of the parent class for maintenance. This inheritance method can implement multiple inheritance, but can only inherit the common methods of the parent class and cannot inherit static methods, you cannot use instanceof to verify the instance.

Function a () {this. say = function () {alert ("happy new year! ") ;}} Function B () {. apply (this, arguments);}. prototype. fuck = function () {alert ("% ^ & % & ^ % &");} var oB = new B (); alert (oB instanceof ); // falseoB. say (); // happy new yearoB. fuck (); // cannot read

Ii. Prototype Inheritance/classic inheritance

This inheritance method allows the object instance to share the attributes of the prototype object by replicating existing prototype objects for behavior reuse. Supports multiple inheritance and inherits the prototype static method. You can use instanceof to verify the instance.

function a(){ this.say=function(){ alert("happy new year!"); }}function b(){}a.prototype.fuck=function(){  alert("%^&%^&%&^%&");}a.prototype.z=123;b.prototype=new a();var oB=new b();alert(oB instanceof a); // truealert(oB.z);      // 123oB.say();        // happy new yearoB.fuck();       // %^&%^&%&^%&

Iii. instance inheritance/parasitic constructor Mode

The constructor cannot inherit static methods of the type, and the prototype cannot inherit well (some non-enumeration methods of core objects cannot inherit). The instance inheritance method can inherit native core objects or DOM objects, it constructs an object in the type and returns a method to implement inheritance. Therefore, the instanceof verification is false and multi-inheritance is not supported.

function a(){ var oA=new Array(); oA.say=function(){   alert("hello A!"); } return oA;}var obj=new a();alert(obj instanceof a); // falseobj.say();

Iv. Copy Inheritance Law

This method simulates inheritance by copying all the enumerated attributes and methods of the base class object. Therefore, it can simulate multi-inheritance, but it cannot inherit without enumeration; it can inherit the static methods of the parent class;

function a(){  this.num=123;  this.say=function(){  alert("happy new year!");  }}function b(){  this.extends=function(obj){    for(each in obj){      this[each]=obj[each];    }  }}var oB=new b();oB.extends(new a());alert(oB instanceof a); // falsealert(oB.num);     // 123oB.say();        // happy new year

V. Mixed Inheritance Law

As the name suggests, it is to combine the above several inheritance laws to learn from each other and improve inheritance. Common constructor + prototype inheritance: pseudo-classic inheritance

function a(){  this.num=123;  this.say=function(){  alert("happy new year!");  }}function b(){  a.apply(this);}b.prototype=new a();b.prototype.z=123;var oB=new b();alert(oB instanceof a); // truealert(oB.num);     // 123oB.say();        // happy new year

Vi. Advantages and disadvantages of various Inheritance Laws

I hope this article will help you design JavaScript programs.

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.