稍后将其他函数添加到类中而不使用 .prototype
在构建类之后,如何引用添加到类中的所有函数? (可能作为数组)并且不使用原型。
我已经构建了一个类 foo()
,并且想添加功能以添加新功能 稍后可以使用。
var f = new foo();
以便
f.addfunc('bar',function(){/*code*/});
稍后可以使用 f.bar();
。
使用 foo.__func_expose(funcname , function );
导出 (?) 函数不起作用。
(可能我做错了。)似乎有一个错误。 (详细信息请参阅下面的代码)。
目的是拥有一个包含所有添加的函数和实际函数的数组,以便以后可以调用/替换它。
在 javascript 中从公共函数中公开新函数名称是否可行?
我想实现一些目标,
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.
目前的实际代码是
function foo(){
// we store reference to ourself in __self
this.__self = arguments.callee;
var self = arguments.callee;
// array that holds funcname , function
if (!self.__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();
}
“是”。我已经做完功课了。我还缺少一些东西。
- 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 框架很好,但在这里我们希望避免依赖,并保持轻量级。
此外,它没有乐趣,是吗:-) 别误会我的意思,我并不反对使用原型。
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.
(probably I am doing it the wrong way.) There seems to be a error. (see code below for detail).
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 (!self.__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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了@Pointy的注释之外,当您将
__self
分配给var self
时,您还缺少this
。当我阅读代码时,__self
是foo
对象的变量,因此在使用它时必须将其引用为this.__self
。顺便说一句,你为什么不使用任何框架呢?当我没记错时,Prototype 框架提供了这样的功能。
编辑:
刚刚帮你查了一下。这个函数应该完全符合你的要求。 AddMethods
Besides the comment of @Pointy you are missing a
this
when you are assigning__self
tovar self
. As I read the code__self
is a variable of yourfoo
object, so you have to reference it asthis.__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