Parse the source code of the Base class inherited by js

Source: Internet
Author: User

// Timestamp: Tue, 01 May 2007 19:13:00
/*
Base2.js-copyright 2007, Dean Edwards
Http://www.opensource.org/licenses/mit-license
*/
// You know, writing a javascript library is awfully time consuming.
/// // BEGIN: CLOSURE ////////////////////
// ================================================ ==========================================
// Base2/Base. js
// ================================================ ==========================================
// Version 1.1
Var Base = function (){
// Call this method from any other method to invoke that method's ancestor
};
Base. prototype = {
Extend: function (source ){
// When the parameter is greater than one
If (arguments. length> 1) {// extending with a name/value pair
// Obtain the ancestor of proto
Var ancestor = this [source];
Var value = arguments [1];
// If the value (the second parameter) is a function and the ancestor object exists
If (typeof value = "function" & ancestor &/\ bbase \ B/. test (value )){
// Get the underlying method
Var method = value;
// Override
Value = function (){
Var previous = this. base;
This. base = ancestor;
// Roll back to the parent class Object
Var returnValue = method. apply (this, arguments );
This. base = previous;
Return returnValue;
};
Value. method = method;
Value. ancestor = ancestor;
}
This [source] = value;
}
Else
If (source) {// extending with an object literal is extended using an object list
Var extend = Base. prototype. extend;
/**
* 1. Extend the prototype method and attributes. 2.
*/
// If you want to extend the methods or attributes of the prototype, first traverse the three methods of Its overloaded Object.
If (Base. _ prototyping ){
Var key, I = 0, members = ["constructor", "toString", "valueOf"];
While (key = members [I ++]) {
// If these methods are overloaded
If (source [key]! = Object. prototype [key]) {
/**
* Extended one by one. The reason for calling is to change the extended context to the extended source. this,
* This is the parent class Object of the new object.
*/
Extend. call (this, key, source [key]);
}
}
}
Else
If (typeof this! = "Function "){
// If the object has a customised extend () method then use it
Extend = this. extend | extend;
}
// Copy each of the source object's properties to this object
For (key in source)
If (! Object. prototype [key]) {
Extend. call (this, key, source [key]);
}
}
Return this;
},
Base: Base
};
Base. extend = function (_ instance, _ static) {// subclass
/**
* Extended alias of the Base class prototype, which is used as a method call
*/
Var extend = Base. prototype. extend;
/**
* Build the prototype to create a prototype.
* Set the prototype flag
*/
Base. _ prototyping = true;
/**
* Create a Base instance and initialize the inheritance part.
* The Inheritance Method is roughly the following:
* Function (){}
* Function B (){
* This. B = [];
*}
* A. prototype = new B (); // A inherits all attributes and methods of B.
* This inheritance method may cause a problem. objects declared in B (such as B) are in prototype format.
* After being inherited by A, prototype only generates A reference to the object in B, that is
* All instances of A will share the objects in B (B)
* Var a1 = new ();
* Var a2 = new ();
* A1. B. push ("a11 ");
* A2. B. push ("a21 ");
* In this case, a1. B = a2. B = ["a11", "a21"],
*
* When implementing inheritance, Dean Edwards creates an instance based on the parent class,
* Extended the instance by using extend, and finally inherited by A. prototype = new B ();
* When the property is an object, no processing is performed,
* The above inheritance defects have not been avoided.
*/
Var proto = new this;
/**
* Here, you cannot use proto. extend (_ instance) instead.
*/
Extend. call (proto, _ instance );
/**
* After the prototype of the class instance attributes and methods is constructed, the flag is deleted.
*/
Delete Base. _ prototyping;
/**
* The author uses the adapter mode to generate a new class object with a custom constructor.
* Wrapper/adapter: through a certain method, one object encapsulates or authorizes another
* Object to change its interface or behavior
*/
// Create the wrapper for the constructor function
/**
* Get the constructor reference
*/
Var constructor = proto. constructor;
/**
* Create a Function object of klass and call a custom constructor. klass is a derived subclass.
* This method is called in two cases:
* 1. When creating a class instance, this is not the prototype construction phase, and the extend Method
* Constructor set during inheritance
* 2. When subclass is derived using the extend method --- new this
* Because all the attributes of klass in the following section have been obtained,
* When new is complete, all methods and attributes of the obtained parent class are included in
* Proto. At this time, the extend method of prototype is used based on proto.
* Add the attributes and methods of this subclass to proto.
*/
Var klass = proto. constructor = function (){
/**
* Var proto = new this; call the constructor of the parent class to create an instance of the parent class
* After new this is used up, the function is redirected to the subclass object constructor.
*/
If (! Base. _ prototyping ){
/**
* When the base method is called in the constructor,
* The base method calls the constructor of the parent class object, which is nested at this time.
* When this code segment is called, the condition that the method can be executed is this. _ constructing = true.
*/
If (this. _ constructing | this. constructor = klass) {// instantiation
This. _ constructing = true;
Constructor. apply (this, arguments );
Delete this. _ constructing;
}
/**
*
* No downstream execution
*/
Else {// casting
Var object = arguments [0];
If (object! = Null ){
(Object. extend | extend). call (object, proto );
}
Return object;
}
}
};
// Build the class interface
/**
*
*/
For (var I in Base ){
Klass [I] = this [I];
}
/**
* Create an inheritance chain
*/
Klass. ancestor = this;
Klass. base = Base. base;
Klass. prototype = proto;
Klass. toString = this. toString;
/**
* Extended class methods and attributes, similar to the java static
*/
Extend. call (klass, _ static );
// Class initialisation if there is an init function call
If (typeof klass. init = "function ")
Klass. init ();
Return klass;
};
// Initialise
Base = Base. extend ({
Constructor: function (){
This. extend (arguments [0]);
}
},{
Ancestor: Object,
Base: Base,
Implement: function (_ interface ){
If (typeof _ interface = "function "){
// If it's a function, call it
_ Interface (this. prototype );
}
Else {
// Add the interface using the extend () method
This. prototype. extend (_ interface );
}
Return this;
}
});

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.