javascript - 如何在 ES6 中导出静态函数?

标签 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() 我该如何实现?

最佳答案

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

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();

关于javascript - 如何在 ES6 中导出静态函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383375/

相关文章:

c++ - 当全局命名空间中的函数声明为静态 C++ 时,这意味着什么?

从另一个类调用方法后,C# 设置 Form Parent

javascript - Vue-bootstrap 导航链接到 'external application'

javascript - JS 如何在 forEach 循环中从数组中删除一个对象?

javascript - 箭头函数的性能和内存占用

c++ 事件队列、可变参数函数、静态函数和其他奇怪的东西

javascript - regex.test() 卡住网页

javascript - 谁能帮我分离世博会应用程序

javascript - 支持 AJAX 历史按钮而不会发疯

javascript - 使用命名空间导入 React 组件