0

What's the benefit of this...

class Utils {
    static doSomething() {
        return 'something'
    }

    static doAnother() {
        return 'another'
    }
}

versus this...

const Utils {
    doSomething: () => 'something',
    doAnother: () => 'another',
}

Assuming Utils is never meant to be instantiated (it's just going to be a collection of methods), is there a reason to use one or the other?

1 Answer 1

1

Assuming Utils is never meant to be instantiated (it's just going to be a collection of methods), is there a reason to use one or the other?

Its different ways of creating the same thing. There are others as well. Since its not meant to be instantiated, I would do the simple const Utils version.

Other ways

e.g. namespace

namespace Utils {
    export function doSomething() {
        return 'something'
    }

    export function doAnother() {
        return 'another'
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah from a consumer's perspective, they are the same thing. You'll use them the same say. As a library developer, isn't there a more concrete way to know which is better knowing that at runtime those different ways actually do the same thing differently?
The simple const version will perform best as its simpler on the runtime as well.
As a library developer, isn't there a more concrete way to know which is better knowing No. It's experience based. Always side with simplicity 🌹

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.