Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
Exporting the object of the class vs exporting the singleton class. Javascript

Is there any difference between the following approaches? I am fond of the second approach as it is more clear but I am seeing Approach 1 also on the internet. So wondering if there any benefit of one approach over other.

Approach 1

Singleton.ts

class Singleton {
    private static _instance: Singleton | null = null
    
    private constructor() {}
    
    public static getInstance() {
        if (!Singleton._instance) {
            Singleton._instance = new Singleton()
        }
        return Singleton._instance
    }
    
    public doSomeWork() {}
}


export default Singleton

test1.ts

import Singleton from './Singleton'

Singleton.getInstance().doSomeWork()

test2.ts

import Singleton from './Singleton'

Singleton.getInstance().doSomeWork()

Approach 2

Singleton.ts

class Singleton {
    constructor() {
        
    }    
    
    public doSomeWork() {}
}


export default new Singleton()

test1.ts

import singleton from './Singleton'

singleton.doSomeWork()

test2.ts

import singleton from './Singleton'

singleton.doSomeWork()
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Second one is preferable I think. First one actually allows instantiating multiple times.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda