About the Javascrip class inheritance encapsulation

Source: Internet
Author: User

The following code is javascrip inherit a piece of inherited code I took the early morning to understand the truth, the most important is that there are some of the usual we do not how to use the functions and methods. More confusing people. Now I'll make some comments about the code: (Of course, he also has some English annotations, but not clear enough, we can also later change this method, make him more suitable for our own use)


/* Simple JavaScript inheritance

* by John Resig http://ejohn.org/

* MIT Licensed.

*/

Inspired by Base2 and Prototype

(function () {

The method initializes the methods and properties in the parent class as if it were inherited, similar to a language with inherited attributes such as Java.

Initializing this property is primarily used as a variable to initialize.

var initializing = false;

This is a piece of code that needs to be treated in a special way, and this code overwrites the parent property with the decision whether it is a function or not, and whether there are any criteria for determining the parent class method. If you have any questions about this, check out another blog post on this code that I reproduced.

Fntest =/xyz/.test (function () {xyz;})? /\b_super\b/:/.*/;

The base Class implementation (does nothing)

The implementation of the base class, this place just declares class base class, do not do anything.

This. Class = function () {};

Create a new class that inherits from this class

This is an inheritance method that declares the class base class, with parameters that require special instructions: Prop is the argument that is passed in when we call the Extend method.

Class.extend = function (prop) {

_super is a particularly useful property. is the array used to store the properties of the parent class. Enables a class that inherits later to invoke the parent class property with This._super.xxx.

var _super = This.prototype;

Instantiate a base class (but only create the instance,

Don ' t run the Init constructor)

initializing = true;

Start by creating your own object, which copies all the properties of the object in prototype, and when you first call Class.extend, the new this () generates a parent object with no special attributes.

var prototype = new This ();


initializing = false;

Copy the properties over onto the new prototype

Here presumably everyone is reading the English comments have figured out what is going on, yes, is the copy attribute. There's only one thing that's been done here. But there is the logic of the cover inside. There is also a link to create a parent class subclass call relationship.

for (var name in prop) {//Loop all properties

Check if we ' re overwriting an existing function

Examine the methods of the parent class, overriding the parent class's methods if the parent class has a method with the same name. In JavaScript, the method parameter is more or less transmitted, a bit like the dynamic parameters in Java. So there is no method polymorphic (overloaded), and the same name is rewritten directly.

Prototype[name] = typeof Prop[name] = = "function" &&

typeof _super[name] = = "function" && fntest.test (Prop[name])?

(function (name, FN) {

return function () {

Temporarily save a reference to your own calling parent class

var tmp = This._super;

Add a new _super () method is the same method

But on the Super-class

Create your own super references

This._super = _super[name];

The method is need to being bound temporarily, so we

Remove it when we ' re done executing

Here comes a very strange method is apply, and this method is very similar to the method has a call method, that is, the FN method to rebind the parameters, this refers to this paragraph Funciton itself. Arguments refers to the parameters that are passed in. That is, whatever parameter is passed in FN, the RET returned here binds the parameter.

var ret = fn.apply (this, arguments);

and set the parent's reference back.

This._super = tmp;

Returns the method call itself

return ret;

};

}) (name, Prop[name]):

prop[name];//if the parent class does not exist then put back the method itself

}

This is the construction method

The Dummy class constructor

function Class () {

All construction is actually do in the Init method

if (!initializing && this.init)

This.init.apply (this, arguments);

}

Assigns the regenerated attribute to class, which is not the base class, but the generated object itself. Which means you can

Populate our constructed prototype object

Class.prototype = prototype;

Constructor reference

Enforce the constructor to being what we expect

Class.prototype.constructor = Class;

Callee This method assigns the extend method of this paragraph to the extend method of class, so that the newly generated class can also implement inheritance

And make this class extendable

Class.extend = Arguments.callee;

return Class;

};

})();


About the Javascrip class inheritance encapsulation

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.