Skip to content Skip to sidebar Skip to footer

Is There Any Advantage To Using Classes With Static Functions Versus Modules?

I would like some advice. I am using typescript to create dialog boxes. Once created they take care of themselves as they have their own submit buttons and do there own checking.

Solution 1:

The module approach is preferred for what you are trying to achieve.

If using the class form, you're effectively defining a constructor function even though you will never new up object instances, and adding members directly on the constructor function that cannot be truly private (as you observed).

Using the module approach you are simply constructing an object, and members you don't export are contained within the closure that sets up the object - giving you true privacy on them.

A good way to see the effects of both is to paste your code for each into http://www.typescriptlang.org/Playground/ and analyze the generated code.

Post a Comment for "Is There Any Advantage To Using Classes With Static Functions Versus Modules?"