Skip to content

Instantly share code, notes, and snippets.

@dmnsgn
Last active February 9, 2023 16:54
Show Gist options
  • Select an option

  • Save dmnsgn/4a6ad76de1b5928f13f68f406c70bb09 to your computer and use it in GitHub Desktop.

Select an option

Save dmnsgn/4a6ad76de1b5928f13f68f406c70bb09 to your computer and use it in GitHub Desktop.

Revisions

  1. dmnsgn revised this gist Apr 16, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion SingletonDefaultExportInstance.js
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,3 @@ console.log(SingletonDefaultExportInstance.type);

    // Static method
    console.log(SingletonDefaultExportInstance.constructor.staticMethod());
    // console.log(const instance = SingletonDefaultExportInstance.constructor);
  2. dmnsgn created this gist Apr 16, 2016.
    42 changes: 42 additions & 0 deletions SingletonDefaultExportInstance.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    class SingletonDefaultExportInstance {
    constructor() {
    this._type = 'SingletonDefaultExportInstance';
    }

    singletonMethod() {
    return 'singletonMethod';
    }

    static staticMethod() {
    return 'staticMethod';
    }

    get type() {
    return this._type;
    }

    set type(value) {
    this._type = value;
    }
    }

    export default new SingletonDefaultExportInstance();

    // ...

    // index.js
    import SingletonDefaultExportInstance from './SingletonDefaultExportInstance';

    // Instantiate
    // console.log(new SingletonDefaultExportInstance); // is not a constructor

    // Prototype Method
    console.log(SingletonDefaultExportInstance.type, SingletonDefaultExportInstance.singletonMethod());

    // Getter/Setter
    SingletonDefaultExportInstance.type = 'type updated';
    console.log(SingletonDefaultExportInstance.type);

    // Static method
    console.log(SingletonDefaultExportInstance.constructor.staticMethod());
    // console.log(const instance = SingletonDefaultExportInstance.constructor);