Hi everyone,
I have this two parts of code which do the same thing.
But for you what is the best ? Using namespace or static class ?
namespace MyMath {
const PI: number = 3.14;
export function calculateCircumference(diameter: number): number {
return diameter * PI;
}
export function calculateRectangle(width: number, length: number): number {
return width * length;
}
}
class MyMathClass {
PI: number = 3.14;
static calculateCircumference(diameter: number): number {
return diameter * PI;
}
static calculateRectangle(width: number, length: number): number {
return width * length;
}
}
Let me know !
Namespaces would be my preference. However, if you are using modules (ES6, AMD, CommonJS, etc.), there is a third, and preferred option, would be to simply export the functions by themselves.
const PI: number = 3.14;
export function calculateCircumference(diameter: number): number {
return diameter * PI;
}
export function calculateRectangle(width: number, length: number): number {
return width * length;
}
With modules the file itself forms a name boundary or "namespace" if you will. (In fact, the keyword namespace is an alias for module in TypeScript.) Writing your code in this manner has some added benefits later on if you use something like tree shaking to eliminate unused code in a package.
You can then import your functions in other files like this:
// If you want to use the prefix
import * as MyMath from './my-math';
// Or without the prefix
import {
calculateCircumference,
calculateRectangle
} from './my-math';
Namespaces are useful if you need to split code across files and want to merge properties under a single parent object. But they are unnecessary is you are using modules already.
The first one generate a object with static functions but the last generate a function with 2 static functions, is not trully a static class and can be initializated.
In the first case PI is protected by context and in the second case will just not work because PI is a field in the MyMathClass instance and is not a static field.
So i say go with the first if you want a more "trully" static or just put static in PI.
With JSComet you can use static fields/properties and use a @static decorator for make a more trully static class (or create one here some samples github.com/cirospaciari/jscomet.decorators/tree/m…) and private vars and functions are context protecteds.
npmjs.com/package/jscomet
First Code Generate:
var MyMath; (function (MyMath) { var PI = 3.14; function calculateCircumference(diameter) { return diameter * PI; } MyMath.calculateCircumference = calculateCircumference; function calculateRectangle(width, length) { return width * length; } MyMath.calculateRectangle = calculateRectangle; })(MyMath || (MyMath = {}));Last Code Generate:
var MyMathClass = /** @class */ (function () { function MyMathClass() { this.PI = 3.14; } MyMathClass.calculateCircumference = function (diameter) { return diameter * PI; }; MyMathClass.calculateRectangle = function (width, length) { return width * length; }; return MyMathClass; }());