如何在ES6中导出静态功能?

时间:2017-11-20 00:02:45

标签: javascript ecmascript-6 static babeljs

基本代码main.js:

class Something {
  static ThisIsStaticFunction () { ... }
}

export default Something;

其他档案xxx.js:

import xxx;

// I want to call `ThisIsStaticFunction()` without having to
// write `Something.ThisIsStaticFunction()` how can I achieve this?

我想拨打ThisIsStaticFunction()而不必写Something.ThisIsStaticFunction()我该如何实现这一目标?

1 个答案:

答案 0 :(得分:5)

您可以将静态函数别名为普通函数

export const ThisIsStaticFunction = Something.ThisIsStaticFunction;
export default Something;
import {ThisIsStaticFunction}, Something from 'xxx';

通常在Javascript(与Java不同)中,您可以使用普通函数而不是静态函数。

export function ThisIsAFunction() {}

export default class Something {
    instanceMethod() {
        const result = ThisIsAFunction();
    }
}
import {ThisIsAFunction}, Something from 'xxx';

const foo = ThisIsAFunction();
const bar = new Something()
const biz = bar.instanceMethod();