如何在javascript/ajax中获取父类的派生类列表? [英] How to obtain list of derived classes for a parent class in javascript/ajax?

查看:68
本文介绍了如何在javascript/ajax中获取父类的派生类列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个javascript类(childA childB),每个都继承自另一个javascript类(Parent).父级包含一个myMethod 函数,该函数被子级覆盖.所有这些类也位于不同的文件中,这些文件已加载到页面上.

我想做的是调用每个派生类的myMethod.我正试图使我的问题非常简短和切题.发生了很多事情,但是我要弄清我的实际问题是什么.

我的问题:
1.我想做的是动态获取从Parent继承的已注册类的列表.我想调用每个派生类的myMethod.
2.从性能的角度来看这会是一个不好的做法吗?

I have two javascript classes (childA and childB), each inheriting from another javascript class (Parent). The parent contains a myMethod function which is overridden by the child classes. All these classes are in different files as well which are loaded on the page.

What I''m trying to do is invoke each of the derived class''s myMethod. I''m trying to keep my question very brief and to the point. There are a bunch of things going on, but I''m zeroing down on what my actual question is.

My questions:
1. What I''d like to do is get a list of registered classes dynamically which inherit from Parent. I would like to invoke myMethod of each of these derived classes.
2. Would this be a bad practice from a performance perspective?

...
Parent.prototype.myMethod  = function(methodName)
{
}



谢谢!



Thanks!

推荐答案

我认为您不明白自己想要什么.在JavaScript中,本身没有类,您只有实例和原型,它们都是对象. JavaScript是一种基于 prototype的脚本语言,它确实支持面向对象的 style ,这与经典的面向对象的范例不同.所有JavaScript对象不过是关联数组/字典.

参见:
http://en.wikipedia.org/wiki/JavaScript [ http://en.wikipedia.org/wiki/JavaScript#Prototype-based [ ^ ].

当您说调用每个派生类的myMethod时,它假定myMethod是经典OOP的 static 方法,否则将需要将某些实例集作为"传递给"".在JavaScript中,没有这样的东西.您只有一组对象,每个对象可能有也可能没有这样的方法.

就是说,每当需要调用某些方法的某些调用集时,只需要一组对象(而不是类!)来调用它即可.要进行这样的设置,您需要将对象放在某个容器中,在for… in…循环中遍历它,并为每个对象调用该方法.如果调用失败,请准备好捕获异常.

关于良好的不良习惯的问题并没有真正的意义,因为您的意思是别的.在循环中调用方法没有错;性能问题也几乎没有意义,因为尚不清楚应该考虑什么作为替代方案以及其目的.

—SA
I think you don''t understand what you want. In JavaScript, there are no classes per se, you only have instances and prototypes, which are all objects. JavaScript is a prototype-based scripting language, which does support of object-oriented style, which is not the same as classical object-oriented paradigm. All JavaScript objects are nothing but associative arrays/dictionaries.

See:
http://en.wikipedia.org/wiki/JavaScript[^],
http://en.wikipedia.org/wiki/JavaScript#Prototype-based[^].

When you say "invoke myMethod of each of these derived classes, it assumes that myMethod is a static method of classic OOP, otherwise it would requires some set of instances to be passed as "this" reference in each call. In JavaScript, there are no such things. You just have some set of object, and each of them may or may not have such method.

That said, whenever you need to invoke some set of calls of some method, you simply need a set of objects (not classes!) to call it. To have such set, you need to put your objects in some container, traverse it in for… in… loop and call the method for each object. Be ready to catch exception if the call fails.

The question about good a bad practice does not really make sense as you meant something else. There is nothing wrong with calling a method in the loop; the question of performance also makes little to know sense as it''s unclear what to consider as an alternative and what''s the purpose of it.

—SA


这篇关于如何在javascript/ajax中获取父类的派生类列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆