OOP extension for Function

Source: Internet
Author: User

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

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.