How to return non static variable from static method ES6 Class

me_digvijay

I have following code:

export class Utils{
    constructor() {
        this.dateFormat = "MM-DD-YY";
    }

    static getFormat() {
        return this.dateFormat;
    }
}

when I am trying to import this class to other file and try to call the static method gteFormat it return undefined. Here is how I am doing it:

import * as Utils from "./commons/Utils.js";

class ABC {
    init(){
        console.log(Utils.Utils.getFormat());// gives undefined
    }
}

How can I make this static method return the dateFormat property?

loganfsmyth

If you are conceptually working with a bunch of functions, you can consider relying on the module scope itself for privacy, rather than the class structure. Then you can export functions or values directly. Like

const dateFormat = "MM-DD-YY";

export function getFormat() {
    return dateFormat;
}

with usage like

import * as Utils from "./commons/Utils.js";

console.log(Utils.getFormat())

or even

import { getFormat } from "./commons/Utils.js";

console.log(getFormat())

or if it's literally a constant you can export it directly

export const DATE_FORMAT = "MM-DD-YY";

then

import { DATE_FORMAT } from "./commons/Utils.js";
console.log(DATE_FORMAT);

Exporting a class with a bunch of static methods is a very Java-y way to write it, and the class itself adds nothing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to access global non static variable from static method

From Dev

how to reffer non static variable from static method?

From Dev

Calling ES6 class constructor from class static method

From Dev

How to reference static method from class variable

From Dev

how to change the value of a static variable from a static method of another class

From Javascript

How do I use a static variable in ES6 class?

From Dev

es6 access static method of child class from static method of parent?

From Dev

How to display the return value from a non static method in C#

From Dev

Call non static method from different non static class

From Dev

How to call non static method from main class

From Dev

How to pass a non static method from a specific class as parameter to Function

From Dev

How to get non-static variable from inherited class?

From Dev

How to return subclass objects from its base class static method?

From

How to call non-static method from static method of same class?

From Dev

If ES6's class static method's this binds to the class, why does this return NaN?

From Dev

Access static variable from non static method in Swift

From Dev

Call a non-static method from static inner class

From Java

Referencing non static variable from within static Inner Class

From Java

How to achieve static method from non-static interface method?

From Java

How to print a non-static method from a static method?

From Dev

How to call a non-static method from a static function in a class in php

From Dev

How to run non static method from static inside single class and use dependency injection C#

From Dev

Anonymous method in static class is non-static? How to invoke it?

From Java

How to instantiate non static inner class within a static method?

From Java

How to change static variable inside in a class by a method

From Dev

How to enter a non static method from a static one

From Dev

How to resolve non-static method errors from static context?

From Dev

Scope of private static variable in non static class

From Dev

How to return parameterized List from a static method?

Related Related

  1. 1

    How to access global non static variable from static method

  2. 2

    how to reffer non static variable from static method?

  3. 3

    Calling ES6 class constructor from class static method

  4. 4

    How to reference static method from class variable

  5. 5

    how to change the value of a static variable from a static method of another class

  6. 6

    How do I use a static variable in ES6 class?

  7. 7

    es6 access static method of child class from static method of parent?

  8. 8

    How to display the return value from a non static method in C#

  9. 9

    Call non static method from different non static class

  10. 10

    How to call non static method from main class

  11. 11

    How to pass a non static method from a specific class as parameter to Function

  12. 12

    How to get non-static variable from inherited class?

  13. 13

    How to return subclass objects from its base class static method?

  14. 14

    How to call non-static method from static method of same class?

  15. 15

    If ES6's class static method's this binds to the class, why does this return NaN?

  16. 16

    Access static variable from non static method in Swift

  17. 17

    Call a non-static method from static inner class

  18. 18

    Referencing non static variable from within static Inner Class

  19. 19

    How to achieve static method from non-static interface method?

  20. 20

    How to print a non-static method from a static method?

  21. 21

    How to call a non-static method from a static function in a class in php

  22. 22

    How to run non static method from static inside single class and use dependency injection C#

  23. 23

    Anonymous method in static class is non-static? How to invoke it?

  24. 24

    How to instantiate non static inner class within a static method?

  25. 25

    How to change static variable inside in a class by a method

  26. 26

    How to enter a non static method from a static one

  27. 27

    How to resolve non-static method errors from static context?

  28. 28

    Scope of private static variable in non static class

  29. 29

    How to return parameterized List from a static method?

HotTag

Archive