Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Exportar el objeto de la clase frente a exportar la clase singleton. JavaScript

¿Hay alguna diferencia entre los siguientes enfoques? Me gusta el segundo enfoque porque es más claro, pero también veo el Enfoque 1 en Internet. Entonces me pregunto si hay algún beneficio de un enfoque sobre otro.

Enfoque 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

prueba1.ts

 import Singleton from './Singleton' Singleton.getInstance().doSomeWork()

prueba2.ts

 import Singleton from './Singleton' Singleton.getInstance().doSomeWork()

Enfoque 2

Singleton.ts

 class Singleton { constructor() { } public doSomeWork() {} } export default new Singleton()

prueba1.ts

 import singleton from './Singleton' singleton.doSomeWork()

prueba2.ts

 import singleton from './Singleton' singleton.doSomeWork()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El segundo es preferible, creo. El primero en realidad permite instanciar varias veces.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!