Add other functions later to a class without using .prototype
How do I reference to all functions added to the class, after the class has been built? (Possibly as a array) and without using prototype
.
I have built a class foo()
, and would like to add the functionality to add new functions
that can be used later.
var f = new foo();
then
f.addfunc('bar',function(){/*code*/});
so that f.bar();
can be used later.
Exporting (?) functions by using foo.__func_expose(funcname , function );
does not work.
The aim is to have an array containing all the functions added and the actual functions, so it can be called/replaced later.
Is it even feasible in javascript to expose new function names from within a public function?
I would like to achieve something along the line of
var MyExtendedFoo = foo();
MyExtendedFoo.__func_add("bar",(function (a, b){
alert('called : anon-func-we-named-bar'); return 2+a+b;}));
MyExtendedFoo.bar(1,3); // <---this does not work.
// It should alert a message, then return 6.
The actual code at the moment is
function foo(){
// we store reference to ourself in __self
this.__self = arguments.callee;
var self = arguments.callee;
// array that holds funcname , function
if (!sel开发者_JAVA技巧f.__func_list_fname) self.__func_list_fname = [];
if (!self.__func_list_func) self.__func_list_func = [];
// allow foo.__func_expose(); register to public
self['__func_expose'] = function(){
var self = __self;
for(var i=0;i<self.__func_list_fname.length;i++){
self[''+self.__func_list_fname[i]+''] = self.__func_list_func[i];
// <---This part seems wrong. How do I do it?
};
return __self.__func_return();
};
// allow foo.__func_add( funcname , function );
self['__func_add'] = function(f,v){
var self = __self;
self.__func_list_fname.push(f);
self.__func_list_func.push(v);
// tell itself to expose the new func as well as the old ones.
return __self.__func_expose();
};
// build obj from known list and return it.
self['__func_return'] = function(){
var self = __self;
var obj = {};
obj['__func_expose'] = self['__func_expose'];
obj['__func_add'] = self['__func_add'];
obj['__func_return'] = self['__func_return'];
for(var i=0;i<self.__func_list_fname.length;i++){
obj[''+self.__func_list_fname[i]+''] = self.__func_list_func[i];
};
return obj;
};
// Return ourself so we can chain
return self.__func_return();
}
Yes. I have done my homework. I am still missing something.
- http://www.crockford.com/javascript/private.html
- https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript
- http://www.stackoverflow.com/questions/55611/javascript-private-methods
@philgiese frameworks are nice, but here we want to avoid dependency, and keep it lightweight.
Besides, there's no fun to it, is there :-) Don't get me wrong, I have nothing against using prototype.Besides the comment of @Pointy you are missing a this
when you are assigning __self
to var self
. As I read the code __self
is a variable of your foo
object, so you have to reference it as this.__self
when you use it.
And by the way, why aren't you using any framework for this? When I remember correctly the Prototype framework offers such functionality.
EDIT: Just looked it up for you. This function should exactly do, what you want. AddMethods
精彩评论