derived 类 可以有静态 get 方法吗?

Can derived classes have static get methods?

更新 2,添加了 Gist

问题:可以派生 classes 有 static get 方法吗?
3 files here,尝试在派生的 class 中使用 static get 方法。在 class' .es6 文件中访问时它可以正常工作,但在导入进行测试时不能正常工作。


我只是在了解 ES6 class 语法,但在 ES5 原型继承中是 strong/comfortable。我正在研究一组树数据结构。使用巴别塔。

我有一个派生的class,RBTree: (见下面的代码)

我希望 RBTree 有一个 static get 方法,它只是作为我已经创建的空节点的 pointer/property。假设我从另一个文件导入了那个节点,它被称为 NULL_POINTER.

如何将此静态获取方法获取到 return NULL_POINTER

当我导入它并尝试像上面那样访问时,.null returns undefined.

我也曾尝试在 class 表达式之外修改 RBTree (RBTree.nullPointer = //...),但这不起作用。


更新 1:
重命名了一些东西。代码已更新以反映命名。 我有一个原始测试尖峰的有效实现。以下是相关文件:

RBTree.es6

import {NULL_POINTER} from 'file/with/null/pointer'
export const _NULL_SENTINEL = new RBNode()// constructs the null node
export class RBTree extends BST {
  constructor() {

//...super call
  }

//...
  static get _nil() {
    return _NULL_SENTINEL;
  }
}

在我的 Jest 测试文件中:

const RBTree = require('../../../src/binary_trees/RBTree');
const RBNode = require('../../../src/binary_trees/RBNode');

当我使用 node-inspector 逐步完成测试时:
1、在RBTree.es6的底部,RBTree._nil可以正常使用
2. 如果我在测试文件中的行之后放置调试器,RBTree._nil returns undefined.
3.RBTree._NULL_SENTINEL是同一个对象

1 和 3 按预期工作。
2背后的原因是,RBTree的static get函数在不同的范围内是运行,不再能访问_NULL_SENTINEL? (由于两个单独的 exports?)

我想在 RBTree 对象上附加一些东西,这样 static get 就可以工作,但我不希望它成为 this.

上的指针

您没有正确导入 classes。由于您使用的是 named 导出,因此您必须使用 named 导入。通过 RBTree._NULL_SENTINEL 可以看出,所有导出都成为模块的属性。 (为什么 _NULL_SENTINEL 是 class RBTree 的静态 属性?你永远不会将它分配给 RBTree


对于 ES6,它将是

import {RBTree} from '../../../src/binary_trees/RBTree';

并使用 CommonJS:

const RBTree = require('../../../src/binary_trees/RBTree').RBTree;