稍后将其他函数添加到类中而不使用 .prototype

发布于 2024-09-30 18:44:33 字数 2873 浏览 1 评论 0原文

在构建类之后,如何引用添加到类中的所有函数? (可能作为数组)并且不使用原型。

我已经构建了一个类 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();
}

“是”。我已经做完功课了。我还缺少一些东西。

@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.

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-10-07 18:44:33

除了@Pointy的注释之外,当您将__self分配给var self时,您还缺少this。当我阅读代码时, __selffoo 对象的变量,因此在使用它时必须将其引用为 this.__self

顺便说一句,你为什么不使用任何框架呢?当我没记错时,Prototype 框架提供了这样的功能。

编辑
刚刚帮你查了一下。这个函数应该完全符合你的要求。 AddMethods

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文