Sign in
Log inSign up

Typescript : Namespace or static class

nicolasb's photo
nicolasb
·Feb 7, 2018

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 !

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct