The last parameter is a JSON-represented class definition
If the argument is greater than one, the first argument is the base class, otherwise the base class is an object
The middle parameter is the interface that the class implements
The return value is a class, the class is a constructor, and inherits the prototype of the base class
function Class () {
Adefine = Arguments[arguments.length-1]; The last parameter passed in is the class to be defined
if (!adefine) return; If no arguments are passed, the direct end is done.
var abase = arguments.length>1?arguments[0]:object; If the passed parameter is greater than 1, the first argument is the base class, and if it equals 1, the base class defaults to Object
function Prototype_ () {}; Create prototype temporary function to hook up the prototype chain
Prototype_.prototype = Abase.prototype;
var aprototype = new Prototype_ (); Build the prototype object to be used by the class
For (var member in Adefine) {
if (Adefine[member]! = ' Create ') {//constructor is not hung on the prototype chain, the constructor is the final return
Aprototype[member] = Adefine[member];
}
}
Set class
if (adefine.create) {//If there is a constructor
var atype = adefine.create; The class is the constructor
}else{//If no constructor
var atype = function () {//Create default constructor
This.base.apply (this, arguments); Calling the constructor of the parent class
}
}
Atype.prototype = Aprototype; To set a prototype for a class (constructor)
Atype.base = abase; Setting Type Relationships
AType.prototype.Type = Atype; Extend a Type property for this class of objects to determine the type of the object
return atype; Return the constructor with the prototype inheritance, and you'll be new.
}
Function object () {}; Defines the object root class for implementing the most basic methods and properties
Object.prototype.isA = function (type) {//Determines whether an object property is of a certain type
var = this. Type;
while (self) {
if (self = = type) {
return true;
}
Self = self. Base;
}
return false;
}
Object.prototype.base = function () {//constructor for calling base class
var Caller = Object.prototype.base.caller; Get the function that called the function
Caller && caller.base && Caller.Base.apply (this, arguments);
If Caller exists and caller.base exists, the Caller.base method is called
If one of the caller or caller.base does not exist, then the subsequent statements do not have to be executed.
}
var people = Class ({//Define people Class
Create:function (name, age) {
This.base ();
THIS.name = name;
This.age = age;
},
Say:function () {
Console.log (' My name is ' + THIS.name + ', I ' m ' + this.age + ');
}
})
var chinapeople = Class (People,
{
Create:function (name, age, country) {
This.base (name, age);
This.country = country;
},
Saycountry:function () {
Console.log (' My country is ' + this.country);
}
}
)
var man1 = new People (' Kaka ', 32);
var man2 = new Chinapeople (' Kaka ', +, ' China ');
Man2.say (); My name is Kaka,i ' m 32.
Man2.saycountry (); My Country is China
Caller does not support opera browser, so overriding the base method
Object.prototype.base = function () {
var Base = this. Type.base; Gets the base class of the current object
if (! Base.base) {//If no base class is already
Base.apply (this, arguments); Call the base class constructor directly
}else{
This.base = Makebase (base); First Carbon this.base
Base.apply (this, arguments);
Delete this.base; Delete a replicated base property
};
function Makebase (Type) {
var Base = type.base;
if (! Type.base) return base//base class has no base class, no need to wrap
return function () {//wrapped as a closure function referencing the temporary variable base
This.base = Makebase (base); First Carbon this.base
Base.apply (this, arguments); When calling the base class constructor
}
}
}
JS Simulation class Inheritance