John Resig's simple JavaScript inheritance learning __javascript

Source: Internet
Author: User
/** * Created by Kbwoniu on 16/6/23.
 * Welcome critical corrections and any form of communication! * */** *simple JavaScript inheritance * http://ejohn.org/blog/simple-javascript-inheritance///** * A general way of inheriting * j There is no standard concept of class in S, the first-class citizen is a function, I still use the concept of class in order to understand and analyze it. The way in which the concepts of the classes are inherited is generally to add variable definitions in the constructor, and then redefine its prototype object prototype as an instance of the base class.
 () {}//base class * SuperClass.prototype.someFunc = function () {* Console.log (' superclass somefunc ');
 * };
 * * Function Subclass () {//derived class *//Variable definition * this.someval = ';
 *} * Subclass.prototype = new superclass ();
 * SubClass.prototype.subFunc = function () {* Console.log (' subclass Subfunc ');
 * };
 * * var sub = new Subclass (); * Sub.somefunc ();//superclass SomeFunc * Sub.subfunc (); Subclass Subfunc * * * two. * To talk about my understanding: * 1.Class understood as base class * 2. Base class classes provide a extend method that can be invoked through a base class to generate a new derived class * 3. Specifies an object that The image contains initialization init (if necessary) for the derived class. And some other extension methods * 4. About override in a derived class: You can override it directly, you can call a method of the base class in a derived class (You must use This._super ()), and the additional content * * is used to implement the idea in an analysis. In order to achieve the above purpose: * 1. Define the class function as the base class, directly defined in the root environment * 2.extend methodis a property of class, not an instance property, and returns an extension class, so you must return a type. Take a look at his practice, using a dummy class as return * 3. Parameter Object * 4. Assign the prototype of a derived class (dummy class) to an instance of the base class. How to reuse the base class method. This is the key.
 A derived class method that defines prototype. * Use This._super to refer to a method of the base class, used as a method of the base class, and replace it with the method of the base class when the actual call. Thus, the method of the Parameter object is updated to a combination of the base class + the newly added content. The way he uses it is to save the prototype of the base class to the _super variable and update the prototype corresponding method, which is redefined using an * anonymous function.
 In a redefined implementation, the This._super method is replaced with the method of the base class before the method of the Parameter object is actually invoked.
 * Closures play a pivotal role in this: to maintain the base class of the prototype object, by using the anonymous function of the immediate call, the parameters of the method of the object of the exquisite encapsulation, for beginners JS * I was really amazing. * 5.prototype has, derivation is implemented * * * three. Attach code comments * Original note deleted, attached to the understanding, the specific use of examples will no longer repeat the * */* Simple JavaScript Inheritance * by John Re
 SIG http://ejohn.org/* MIT licensed. */
(function() {///test the regular expression, get a _super regular expression object to check if the method of the base class is usedvarinitializing =false, fntest =/xyz/.test (function() {xyz;}) ?

  /\b_super\b/:/.*/; Base class This. Class =function(){}; Extend Method class.extend =function(prop) The prototype of the {//Paucunqui class, this is the base class type, is the name of the functionvar_super = This. prototype; Defines an instance of a base class that, in order to prototype the derived class, skips the Init method directly, and the Init method can be understood as a private method of each class and is not used for derivation initializing =true;varPrototype =New This(); initializing =false; Re-assign values to the prototype property for(varName inProp) {Prototype[name] =typeofProp[name] = = "function" &&//Must be a functiontypeof_super[name] = = "function" && fntest.test (Prop[name])? The _super method must be used (function(Name, FN) {return function() {//Save original objectvarTMP = This. _super; A method that adds a _super method and assigns a value to the base class This. _super = _super[name]; To really invoke a derived class method, the This._super in the derived class method has just been updated for the method of the base classvarret = fn.apply ( This, arguments); Restore This. _super = tmp; The return value is still a derived call return value returnRet
        }; }) (name, Prop[name]): prop[name];//Conditional Statement}//dummy ClassfunctionClass () {//All construction is actually do in the Init methodif(!initializing && This. Init) This. init.apply ( This, arguments);

    }//Update prototype Class.prototype = prototype;

    Because the above sentence will let it not find constructior Class.prototype.constructor = Class;

    Yes, it is also a extend method of class.extend = Arguments.callee; Returned the derived class returnClass;
}; })();
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.