You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are storing the arrays of months and days as instance properties. That means that every instance will own their own array. This is an obvious solution but looking deeper the arrays will be duplicated for each instance of this class. Seems like there is no need to copy these since they are not changed or modified for each instance!
Here are a couple possibly better solutions.
Put the arrays of months and days on a class property. D.months = ['Jan', ... ]. Now there is only one copy of the array ever created.
A better and more modern solution would be modules. Create a new file that defines the months and days arrays and export them. Any files that need these arrays can import them. This solution sounds almost too simple but I think it's the best in this situation.
// helpers.jsexportconstmonths=['Jan', ...]
// index.jsimport{months}from'./helpers'
Having a "months" variable floating around might seem like an issue but in the world modules these variables will scoped to the module! So they are only available in the module where it is defined, and in the modules that import it.
Also, the code in modules is only defined once no matter how many times it's imported.
The downside to using a module is the array passed around by reference. This means it would be possible for one of the importers to modify the array and this would affect all importers. Since you're using TypeScript you could make your arrays ReadonlyArray!
The text was updated successfully, but these errors were encountered:
date-lib-ts/src/index.ts
Line 3 in 8a170be
You are storing the arrays of months and days as instance properties. That means that every instance will own their own array. This is an obvious solution but looking deeper the arrays will be duplicated for each instance of this class. Seems like there is no need to copy these since they are not changed or modified for each instance!
Here are a couple possibly better solutions.
Put the arrays of months and days on a class property.
D.months = ['Jan', ... ]
. Now there is only one copy of the array ever created.A better and more modern solution would be modules. Create a new file that defines the months and days arrays and export them. Any files that need these arrays can import them. This solution sounds almost too simple but I think it's the best in this situation.
Having a "months" variable floating around might seem like an issue but in the world modules these variables will scoped to the module! So they are only available in the module where it is defined, and in the modules that import it.
Also, the code in modules is only defined once no matter how many times it's imported.
The downside to using a module is the array passed around by reference. This means it would be possible for one of the importers to modify the array and this would affect all importers. Since you're using TypeScript you could make your arrays
ReadonlyArray
!The text was updated successfully, but these errors were encountered: