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

153
Views
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 answers
Answer question

0

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

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!