Copy codeThe Code is as follows:
// The following is the method used by OOP
// This is very cumbersome ...... Because JS is not an OOP language ......
// But the great fans have guided us to do this.
// Belldandy will bless those who use these methods for OOP ......
Function. prototype. inherits = function (base ){
// Derivative relationship, with prototype retained
// Only single derivation is supported
This. prototype = new base ();
Return this;
}
Function. prototype. create = function (){
// Class creator, equivalent to using new
// JS does not support using call and apply in the constructor, so ......
// Belldandy. Thank you for telling me how to solve this problem ......
Var _ args = [];
For (I = 0; I <arguments. length; I ++) _ args. push ('arguments ['+ I +'] ');
Return eval ('new this ('+ _ args. join (', ') +'); // eval is used ...... Bell, give me a better idea next time ......
}
Function. prototype. pin = function (pinner, args ){
// Register the service, or the "pin" Service
// EventManager can do this.
// You can also think that the interface with default implementation is implemented ......
// For example, pin EventManager can be like this: Class. pin (core. wvwnmanager)
Args = args | [];
Pinner. apply (this. prototype, args );
Return this;
}
Function. prototype. method = function (name, f) {// Add method, efficient
If (! (F instanceof Function) throw new Error ('method binding is invalid, and the type '+ typeof f +' is obtained; expected to be function ');
This. prototype [name] = f;
Return this
}
Function. prototype. property = function (name, localName, getter, setter) {// you can customize the attributes of getter and setter.
If (! Name |! Name instanceof String) throw new EnvironmentException ('Property name is not defined or is not string ');
If (! LocalName |! LocalName instanceof String) localName = '_ local _' + name;
If (getter instanceof Function ){
This. prototype ['_ belldandy_get _' + name] = getter;
}
If (setter instanceof Function ){
This. prototype ['_ belldandy_set _' + name] = setter;
}
This. prototype [name] = new Function ("value, force ","\
If (! Value &&! Force ){\
If (! This ['"+' _ belldandy_get _ '+ name +"'] |! This ['"+' _ belldandy_get _ '+ name +"'] instanceof Function )\
Return this ['"+ localName +"'];/* When getter is not set */\
Else \
Return this ['"+' _ belldandy_get _ '+ name +"']. call (this );\
} Else {\
If (! This ['"+' _ belldandy_set _ '+ name +"'] |! This ['"+' _ belldandy_set _ '+ name +"'] instanceof Function )\
This ['"+ localName +"'] = value ;\
Else \
This ['"+' _ belldandy_set _ '+ name +"']. call (this, value );\
Return this \
} ") // Belldandy. Forgive me, though no closure is generated.
Return this;
}
Function. prototype. static = function (name, value) {// static features, including attributes and Methods
This [name] = value;
Return this;
}
The effect is as follows:
Copy codeThe Code is as follows:
Function foo (){};
Foo
. Property ('A', '_ ')
. Property ('B', '_ B', function () {return this. _ B + '.'})
. Method ('F', function () {dwn (this. ())});
Function bar (x, y) {this. x = x; this. y = y ;};
With (bar ){
Inherits (foo)
Method ('G', function () {dwn (this. a () + '-' + this. B ())})
}
Var f = new foo ();
F. a (1 );
F. B (2 );
Dwn (f. ());
Dwn (f. B ());
F. f ();
B = bar. create (1, 2 );
B. a (4 );
B. B (5 );
Dwn (B. x + ',' + B. y );
B. g ();
// Dwn read the book of shadow by yourself