0

If I were to create the following namespace and class in Typescript, what difference would there be between the two? Does the memory management differ between the two?

export namespace TestNamespace {
  export const Test = 'TEST';
  export const TestFunction = (value:string):boolean => {
      return !value || value.length === 0;
  }
}

export class TestClass {
    static Test = 'TEST';
    static TestFunction(value:string): boolean {
        return !value || value.length === 0;
    }
}

They can both be called in the same way:

console.log(TestClass.Test);
console.log(TestClass.TestFunction('Test'));

console.log(TestNamespace.Test);
console.log(TestNamespace.TestFunction('Test'));

Is the only difference just the preference in how it's written?

1
  • 1
    it all depends on the purpose. There are some differences between classes and namespaces you can find these easily on the web. In your example there is no difference. So it's not a preference on how the code is written but it depends on what you need to use in a specific scenario. Commented Sep 11, 2019 at 7:10

2 Answers 2

1

There are a couple of differences, but they're probably not particularly significant:

  • The most obvious difference between them is that TestClass is a constructor function and TestNamespace is a non-function object. You can call the constructor function. You can't call an object. (You could minimize that difference by making it abstract so new TestClass would be disallowed by the TypeScript compiler.)

  • If you're targeting ES2015+, the class syntax will remain class syntax in the compiled JavaScript. (If you're targeting ES5 or earlier, it's compiled to a function.)

There shouldn't be any significant memory management difference.

Sign up to request clarification or add additional context in comments.

2 Comments

An abstract class with static members only would still be the wrong tool for the jobb though ...
@JonasWilms I don't disagree. In OO languages like Java you do occasionally get a "utility class" that just has static methods and is disallowed from instantiation (private constructor or abstract class) but in those instances, it's a kludge around the fact that you can't just have free floating functions. Better OO design would be to have an utility class which can produce an instance and you use the utility methods from that. DI would be my go-to for ensuring you don't litter memory with instances but a Singleton can work. Still in JS you should be using functions if you don't need classes.
1

TestClass is a class, thus you can create instances from it. It doesn't seem as if you need that, so go with the namespace.

Does the memory management differ between the two?

Not really, both will create an object that lives forever.

Comments

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.