SoFunction
Updated on 2025-03-01

OOP extension for Function


// Here is the method used by OOP
// This is very vulgar... because JS is not an OOP language...
// But the great Wu fans guide us to do this
// Belldandy will bless people who use these methods to OOP...
= function(base){
//Derived relationship, retaining prototype
//Only single derivatives are supported
= new base();
return this;
}
= 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<;i++) _args.push('arguments['+i+']');

return eval('new this('+_args.join(',')+')'); //Eval is used... Bell, give me a better idea next time...
}
= function(pinner,args){
// Register service, or "pin" service
// EventManager can do this
// You can also think that the interface with default implementation is implemented...

// For example, pin EventManager can do this: ()
args = args || [];
(,args);
return this;
}
= function(name, f) { //Add method, efficient
if (!(f instanceof Function)) throw new Error('The method binding is invalid, and the type '+typeof f+' is obtained; expect function');
[name] = f;
return this
}
= function(name, localName, getter, setter) { //Add attributes, you can customize getter and setter
if (!name || !name instanceof String) throw new EnvironmentException('When defining the attribute, the attribute name is not defined, or it is not a string');
if (!localName || !localName instanceof String) localName = '_local_' + name;
if(getter instanceof Function) {
['_belldandy_get_'+name] = getter;
}
if(setter instanceof Function){
['_belldandy_set_'+name] = setter;
}
[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 will not create closures
return this;
}
= function(name,value){ // Static features, including properties and methods
this[name] = value;
return this;
}