Skip to content

Instantly share code, notes, and snippets.

@mohammadhb
Last active August 3, 2022 15:54
Show Gist options
  • Save mohammadhb/a9d52562fae63e722adea13c6e9d32eb to your computer and use it in GitHub Desktop.
Save mohammadhb/a9d52562fae63e722adea13c6e9d32eb to your computer and use it in GitHub Desktop.

Revisions

  1. mohammadhb revised this gist Aug 3, 2022. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions singleton.new.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    //Works with new
    class Counter {

    static instance;
    constructor(x){
    this.x = x
    if(!Counter.instance) Counter.instance = this;
    return Counter.instance;
    }

    counter = 0;
    getCount() {
    return this.counter;
    }

    increment() {
    this.counter = this.counter + 1;
    return this.counter;
    }
    }

    module.exports = Counter;
  2. mohammadhb created this gist Aug 3, 2022.
    20 changes: 20 additions & 0 deletions singleton.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    class Counter {

    static instance;
    static getInstance(){
    if(!Counter.instance) Counter.instance = new Counter();
    return Counter.instance;
    }

    counter = 0;
    getCount() {
    return this.counter;
    }

    increment() {
    this.counter = this.counter + 1;
    return this.counter;
    }
    }

    module.exports = Counter.getInstance();