201506021403_ "JavaScript Perfect Nectar Model Code"

Source: Internet
Author: User

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"

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.