JavaScript Perfect Nectar Model Code
function Class () {
var adefine = arguments[arguments.length-1];
if (!adefine) return;
Parsing base Classes
var abase = arguments.length > 1?arguments[0]:object;
Temporary functions for hooking up the prototype chain
function Prototype_ () {};
Prepare to pass base class
Prototype_.prototype = Abase.prototype;
Create a class to use prototype
var aprototype = new Prototype_ ();
Copy the class definition to the current prototype
for (var menber in Adefine)
constructors do not copy
if (menber!= "Create")
Aprototype[menber] = Adefine[menber];
Note the following statements individually, depending on whether you inherit special attributes or performance conditions
if (adefine.tostring! = Object.prototype.toString)
aprototype.tostring = adefine.tostring;
if (adefine.tolocalestring! = Object.prototype.toLocaleString)
aprototype.tolocalestring = adefine.tolocalestring;
if (adefine.valueof! = Object.prototype.valueOf)
aprototype.valueof = adefine.valueof;
If there is a constructor function
if (adefine.create) {
var atype = adefine.create;
}else
Atype = function () {
Call the constructor of the base class
This.base.apply (this,arguments);
};
Set the prototype of a class
Atype.prototype = Aprototype;
Set type relationships to trace inheritance relationships
Atype.base = abase;
Extend a Type property for this class of object
AType.prototype.Type = Atype;
Returns a constructor as a class
return atype;
};
Root class object definition
Defines the lowercase object root class for implementing the most basic method
Function object () {};
Object.prototype.isA = function (atype) {
var = this. Type;
while (self) {
if (self = = Atype) return true;
Self = self. Base;
};
return false;
};
Call the base class constructor
Object.prototype.base = function () {
Gets the base class of the current object
var Base = this. Type.base;
The destructors class already has no base class
if (! Base.base) {
Base.apply (this,arguments);
}else {
The Destructors class also has a base class
1. Write First This.base
This.base = Makebase (base);
2. Call the constructor of the base class again
Base.apply (this,arguments);
3. Delete the overridden base property
Delete this.base;
};
Wrapper constructor for base class
function Makebase (Type) {
var Base = type.base;
If the base class already has no base class, there is no need to wrap
if (! Base.base) return Base;
Otherwise, the constructor for the temporary variable base is applied
return function () {
1. Write This.base First
This.base = Makebase (base);
2. Call the constructor of the base class again
Base.apply (this,arguments);
};
};
};
/**
*
*==================== using =========================
*/
var person = Class ({
Create:function (name,age) {
Call the upper-level constructor
This.base ();
THIS.name = name;
This.age = age;
},
Sayhello:function () {
Alert ("Hello I ' m" + THIS.name + "," + This.age + "Years old!");
},
Tostring:function () {
Overwrite ToString Method
return this.name;
}
});
var Employee = Class (person,{
Create:function (name,age,salary) {
Call the constructor of the base class
This.base (Name,age);
This.salary = salary;
},
Showmethemoney:function () {
Alert (this.tostring () + "$" + this.salary);
}
});
var xijinping = new Person ("xijinping", 63);
var Likeqiang = new Employee ("Likeqiang", 55,3500);
201506021403_ "JavaScript Perfect Nectar Model Code"