Home  >  Article  >  Web Front-end  >  OOP extension for Function_javascript skills

OOP extension for Function_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:53:191015browse
Copy the code The code is as follows:

// The following is the method used by OOP
// This is very convenient It's obscene...because JS is not an OOP language...
// But the great fans guide us to do this
// Belldandy will bless those who use these methods to OOP...
Function.prototype .inherits = function(base){
//Derivation relationship, prototype is retained
//Only single derivation is supported
this.prototype = new base();
return this;
}
Function.prototype.create = function(){
//The creator of the class is equivalent to using new
//JS does not support the use of call and apply in the constructor, so...
/ /Belldandy, thank you for telling me how to solve this problem...
var _args = [];
for(i=0;i
return eval('new this(' _args.join(',') ')'); //eval is used...Bell, please be nicer next time My idea...
}
Function.prototype.pin = function(pinner,args){
// Register service, or "pin" service
// EventManager can do this
// You can also think of it as implementing an interface with a default implementation...

// For example, pin EventManager can be like this: Class.pin(core.WvwntManager)
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('Invalid method binding, got type ' typeof f '; expected function');
this.prototype[name] = f;
return this
}
Function.prototype.property = function(name, localName, getter, setter) { //Add properties, you can customize getters and setters
if (!name || !name instanceof String) throw new EnvironmentException('When defining the attribute, the attribute name is not defined or is not a 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 no getter is 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, although this does not produce a closure
return this;
}
Function.prototype.static = function(name,value){ //Static features, including attributes And the method
this[name] = value;
return this;
}

has the following effect:
Copy code The code is as follows:

function foo() { };
foo
.property('a', '_a')
.property('b', '_b', function() { return this._b '.' })
.method('f', function() { dwn(this.a ()) });
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.a());
dwn(f.b());
f.f();
b = bar.create(1,2 );
b.a(4);
b.b(5);
dwn(b.x',' b.y); >
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn