The best extend Inheritance Method in Javascript

Source: Internet
Author: User

I have never written a serious article. I want to change it today ..

Inheritance in Javascript is nothing more than a new function, called constructor. It doesn't matter what it is called. It's all function functions,

Then add some methods to the prototype of this method. These methods are inherited.

Javascript has the following concept: "Everything is an object", that is, JavaScript is an object,

An object has two types of attributes, one being inherited and the other being its own,

Just like animals in nature, from the most basic crawler to the present human beings, they all inherit and expand from generation to realize a rich and diverse world. The world of JavaScript also simulates the world of nature, similar to other languages, the world of software is simulating learning the laws of nature.

Back to the question, there is no standard Inheritance Method in the Javascript language,

What is a standard Inheritance Method? It is used by a language to inherit. Such as PHP:
Ancestor {//There are a lot of methods and attributes written here, private and common.Function_ Construct ($ options) {$This-> Options =$ Options ;}FunctionRun (){Return1;}};

 

For the above syntax, you can ignore PHP that you don't understand.CodeAfter writing it, you can create a new class and return an object. The above methods and attributes can be used in the object $ obj.

 
$ OBJ =NewAncestor (1 );

 

What about inheritance?

Child extend ancestor {//Write a lot of method attributes, which can be private or public.Function_ Construct ($ options, $ name ){//Run the _ Construct Method of the parent class.Parent: :__ construct (); $This-> Name =$ Name ;}FunctionRun () {$ num=Parent: :__ construct ();Return$ Num + 1; }}$ Childobj=NewChild (1, 'one Conan ');

 

The above code is the method and attribute that child inherits ancestor .. The instantiation is new.

 

We can note that when the child and ancestor methods are repeated, we can useParent: Method NameTo execute the methods of the parent class, so that you can not overwrite the methods of the parent class, but also expand some methods by yourself, which is very convenient...

 

The key is,

This is implemented in JavaScript.

 VaR Ancestor = Function  (Options ){  This . Options = Options;  This . Run = Function  (){  Return 1 ;}}  // Instantiate class  VaR OBJ = New  Ancestor;  //  Write a constructor child  VaR Child = Function  (){  This . Run = Function  (){  Return 2 ;}}  // Let child inherit Ancestor
 
Child. Prototype =NewAncestor;
 
VaR childobj=NewChild (1, 'one Conan');
 
//After the child is inherited, the child's run method will certainly overwrite the run method in prototype.
The problem arises.

1. We do not have a standard method here. First, we call the method with the same name as the parent class.

2. Write child in this way. prototype = new ancestor; very troublesome and non-standard. For large projects, it is not uniform. Some may be child. prototype. run = function () {} and so on ..

There are some solutions to the first problem. Good, I personally think it is awkward, that isOBJ. Run. Apply (this, arguments );

 VaR Ancestor = Function  (Options ){ This . Options = Options;  This . Run = Function  (){  Return 1 ;}}  //  Instantiate class  VaR OBJ = New  Ancestor;  //  Write a constructor child  VaR Child =Function  (){  This . Run = Function  (){  //The run method of the parent class is called first, but it is unfriendly to directly write obj.VaR num =OBJ. Run. Apply (This, Arguments );  Return Num + 1 ;}}  //  Let child inherit classh Child. Prototype = OBJ;  VaR Childobj = New Child (1, 'one Conan' );  //  After the child is inherited, the child's run method will certainly overwrite the run method in prototype. 

 

 

We need a unified Inheritance Method to Solve the above problem. I have provided a detailed comment for you,

Okay,

In fact, the above is written so as to bring out its meaning:

( Function  (Win, undefined ){  VaR Initializing = False  , Supertest =/Vnice/. Test ( Function () {Vnice ;})? /\ B _super \ B /:/.*/ ;  This . Class = Function  () {}; Class. Extend = Function  (PROP ){  //  _ Super and prototype: Create a new object. As prototype of the new class, you cannot directly add a method to it, which affects other classes returned by using the extend method.          VaR _ Super = This  . Prototype;  //  If it is set to true, the init method is no longer executed. Initializing = True  ;  VaR Prototype = New   This  (); Initializing = False  ;  //  Copy the methods in the passed prop object to prototype.          For ( VaR Name In  Prop) {prototype [name] = (Typeof Prop [name] === 'function '&& Typeof _ Super [name] === 'function' & supertest. Test (prop [name])? ( Function  (Name, FN ){  //  To implement this step, a method in prop (such as prop. init), you can call [this. _ super ()], where this. replace the _ super method with the prototype (prototype. init ).                      Return   Function  (){  VaR Temp = This . _ Super;  This . _ Super = _ Super [name];  VaR Ret = fn. Apply ( This  , Arguments );  This . _ Super = Temp;  Return  RET ;}}) (name, prop [name]): prop [name];}  Function  Class (){ //  This. init method is called by default in the constructor.              If (! Initializing && This  . Init ){  This . Init. Apply ( This  , Arguments );}}  //  Inherit prototype from the class to be returned Class. Prototype = Prototype;  //  Set the class constructor to class. Class. constructor =Class;  //  Add the extend Method to the class to facilitate inheritance. Class. Extend = Arguments. callee;  //  Returns the constructor.          Return  Class ;};}) (window, undefined) 
How can I use the above method ???
( Function  (){  //  Class implementation  VaR Ancestor = Class. Extend ({init: Function  (Options ){  This . Options = Options;}, run:  Function  (){  Return 1 ;}});  //  Instantiate class  VaR OBJ = New  Ancestor ();  VaR Child =Ancestor. Extend ({init:  Function  (Options, name ){  //  Execute the init method of the parent class, similar to parent: Init () in PHP ();            This  . _ Super ();  This . Name = Name}, run:  Function  (){  VaR Num = This  . _ Super (); Return Num + 1 ;}}); Var childobj = New Child (1, 'one Conan' );})() 

 

In this way, we can continue to extend the inheritance .. If you say something wrong, you must correct it...

 

 

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.