Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I export static function in ES6?

Basic codes main.js:

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

export default Something;

Other file xxx.js:

import xxx;

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

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

like image 207
notalentgeek Avatar asked Jan 04 '23 04:01

notalentgeek


1 Answers

You can alias the static function as a normal function

export const ThisIsStaticFunction = Something.ThisIsStaticFunction;
export default Something;

import {ThisIsStaticFunction}, Something from 'xxx';

Usually in Javascript (unlike Java) you can use plain function over static function.

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();
like image 145
Bryan Chen Avatar answered Jan 09 '23 13:01

Bryan Chen