如何在 CoffeeScript 或 Javascript 中列出继承类的方法

发布于 2024-12-05 03:14:56 字数 662 浏览 2 评论 0原文

示例:

  class Complex 
    constructor: (@a, @b) ->
    conjugate: -> new Complex(@a, -@b)

  class ComplexSon extends Complex
    constructor: (@a, @b) ->
      @c = 3.14
    magnitude: -> @a*@a + @b*@b

我定义了以下方法:

  dumpMethods = (klass) ->
    Object.getOwnPropertyNames(klass.property).sort()

测试用例:

  dumpMethods(Complex) == ['conjugate', 'constructor']   
    # success

  dumpMethods(ComplexSon) == ['conjugate', 'constructor', 'magnitude'] 
    # fails, returns                      ['constructor', 'magnitude'] 

dumpMethods 的正确定义是什么?

Example:

  class Complex 
    constructor: (@a, @b) ->
    conjugate: -> new Complex(@a, -@b)

  class ComplexSon extends Complex
    constructor: (@a, @b) ->
      @c = 3.14
    magnitude: -> @a*@a + @b*@b

I have defined the following method:

  dumpMethods = (klass) ->
    Object.getOwnPropertyNames(klass.property).sort()

Test cases:

  dumpMethods(Complex) == ['conjugate', 'constructor']   
    # success

  dumpMethods(ComplexSon) == ['conjugate', 'constructor', 'magnitude'] 
    # fails, returns                      ['constructor', 'magnitude'] 

What is the correct definition of dumpMethods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

神魇的王 2024-12-12 03:14:56

禁止,

Javascript(以及咖啡脚本)使用原型对象。

我建议你阅读一下它,因为事情相当复杂。

总结一下,每个对象都有一个原型。原型本身就是一个对象,有自己的属性,也有原型等等。

原型链实际上定义了类层次结构。因此,在您的情况下,ComplexSon 将有一个 Complex 原型,该原型将有一个 Object 本身的原型,即 JavaScript 中所有对象层次结构的根。

当您在实例上调用方法时,JavaScript 将在该实例上搜索该方法,然后在其原型中搜索,然后在链上搜索。第一个找到的就是它将执行的方法。

与大多数编程语言一样,您可以“向上”层次结构并查看超类,但很少可以向下,因为语言解释器本身很少需要它。然而,有一些解决方法,比如原型使用的方法,可以了解给定类的子类,但据我所知,它们不在语言本身中,大多数情况下它们只是简单地跟踪定义的类。

关于方法,在您的代码中,您正在查看 ComplexSon 的属性,它正确地仅包含两个方法。另一个(coniugate)不存在,因为它是通过原型到达的,您可以通过递归地沿着原型链向上列出它们。

ban,

Javascript (and consequently coffee script) use prototypal objects.

I suggest you read about it because the matter is rather complex.

Trying to summarize, each object has a prototype. The prototype is itself an object, has its own properties, and also has a prototype, and so on.

The chain of prototypes actually defines the class hierarchy. So in you case, ComplexSon will have a prototype that is Complex, that will have a prototype that is Object itself, the root of all object hierarchies in javascript.

When you call a method on an instance, javascript will search for that method on that instance, then in its prototype, then up on the chain. The first one found is the method it will execute.

As in most programming languages, you can go "up" the hierarchy and see superclasses, but rarely you can go down cause it is rarely needed by the language interpreter itself. However there are some workarounds, like ones used by prototype, to know the subclasses of a given class, but AFAIK they are not in the lauage itself, most often they simply keeps track of defined classes.

Regarding the methods, in your code you are looking at the properties of ComplexSon, that correctly consist of only two methods. The other one (coniugate) is not there cause it is reached via the prototype, you can list them all by recursively going up the prototype chain.

似最初 2024-12-12 03:14:56

根据詹尼的回答,我最终得到了以下实现。
dumpMethods 遍历根类 Object,但避免列出 Object 中的方法。

  dumpMethods = (klass) ->
    res = []
    k = klass.prototype
    while k
      names = Object.getOwnPropertyNames(k)
      res = res.concat(names)
      k = Object.getPrototypeOf(k)
      break if not Object.getPrototypeOf(k) # suppress Object
    res.unique().sort()

Based on Gianni's answer I ended up with the following implementation.
dumpMethods traverses against the root class, Object, but avoids listing the methods in Object.

  dumpMethods = (klass) ->
    res = []
    k = klass.prototype
    while k
      names = Object.getOwnPropertyNames(k)
      res = res.concat(names)
      k = Object.getPrototypeOf(k)
      break if not Object.getPrototypeOf(k) # suppress Object
    res.unique().sort()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文