1

I would like to understand what will be the difference in JS if I will call a function from an OOP including "instance" before calling the key/instance inside.

EDIT after solved:

I need to use the class as singleton but as @VLAS answer below when you export the class as default and add new it will make it singleton automatically. here is the syntax:

class Person {
    ...
}
export default new Person() //this syntax should make it singleton

the code before the edit for the explain of my issue:

import Person from './personManager';
const person = new Person()
person.introduceSelf()
person.instance.introduceSelf()

I think its a syntax from ES5 that now in ES6 we are don't need any more? or there is something more complex that I didn't understand?

in our project we have this code I just added the personManager.js for the example:

syngletonSymbol.js

/* eslint-disable */
let SingletonSymbol;
if (!SingletonSymbol) {
  SingletonSymbol = (function (Object) {
    let ObjectPrototype = Object.prototype,
      defineProperty = Object.defineProperty,
      prefix = `__simbol${Math.random()}__`,
      id = 0;

    function get() { /* avoid set w/out get prob */ }

    function SingletonSymbol() {
      const __symbol__ = prefix + id++;
      defineProperty(
        ObjectPrototype,
        this._ = __symbol__,
        {
          enumerable: false,
          configurable: false,
          get, // undefined
          set(value) {
            defineProperty(this, __symbol__, {
              enumerable: false,
              configurable: true,
              writable: true,
              value
            });
          }
        }
      );
    }

    defineProperty(SingletonSymbol.prototype, 'toString', {
      enumerable: false,
      configurable: false,
      writable: false,
      value: function toString() {
        return this._;
      }
    });

    return SingletonSymbol;
  }(Object));
}

export default SingletonSymbol;

singleton.js

/* eslint-disable */
const SingletonSymbol = require('./singletonSymbol');

export default class Singleton {
  static get instance() {
    if (!this[SingletonSymbol]) {
      this[SingletonSymbol] = new this();
    }

    return this[SingletonSymbol];
  }

  constructor() {
    const Class = new.target ? new.target : this.constructor;

    if (!Class[SingletonSymbol]) {
      Class[SingletonSymbol] = this;
    }

    return Class[SingletonSymbol];
  }
}

personManager.js

import Singleton from 'infrastructure/helpers/singleton';
class Person extends Singleton{
  name;
  constructor() {
    super()
    this.name = 'John';
    this._init();
  }
  _init = () => {
      this.name = 'Mario';
  }
  introduceSelf() {
    console.log(`Hi! I'm ${this.name}`);
  }

}

I will appreciate any answer that will help me to understand the idea and if all that singleton is something that is deprecated and can be deleted from my project

Jovannni
  • 21
  • 1
  • 5
  • Singleton was never needed if you had a module in place. You can just export an instance instead of the whole class. It's a singleton in that case. – VLAZ Mar 17 '22 at 11:45
  • @VLAZ when I take out the extends Singleton and call the function `person.introduceSelf()` I get this error: `TypeError: personManager__WEBPACK_IMPORTED_MODULE_6__.default.introduceSelf is not a function` – Jovannni Mar 17 '22 at 12:26
  • `SingletonSymbol`, wth? Why don't they just use a normal `Symbol()`? It's not like this code was transpiled to ES5, was it? – Bergi Mar 17 '22 at 14:13
  • a) don't use singletons b) [don't use `class`es for them](https://stackoverflow.com/a/39079929/1048572) c) the `Singleton` class seems broken. You can call `new Person()` twice and will get the same instance back, but it will run the initialisation twice - resetting the instance on the second call. – Bergi Mar 17 '22 at 14:18
  • Btw `person.instance.introduceSelf()` doesn't work, you probably meant `Person.instance.introduceSelf()` – Bergi Mar 17 '22 at 14:19
  • Hi @Bergi yes as I edited the question there is no need to all the singleton files.. its an old code, when I changed the syntax of the class file at the bottom to: `export default new Person()` it did what I expected thanks – Jovannni Mar 17 '22 at 14:30
  • @Jovannni Yeah, that's much better than `extends Singleton`, but I still would recommend to avoid it. – Bergi Mar 17 '22 at 14:34

0 Answers0