Ad
  • Default User Avatar

    thanks but why if (!Singleton.a) , why not just define the property without the if statement.

  • Custom User Avatar

    functions are objects, so we are defining the function Singleton and additin a propery 'a'. I've called it 'a' to make a point that its arbitrary, could be named anything. sometimes people name this property instance or instance which makes it look like there is some magic going on, when its not.

    So when the funciton is called the first time and 'a' is undefined/falsy we'll store the function in a. So we are storing a reference to the function in a field on the same function, sounds a bit weird but it allows us to consitently return the same (functon) object is all subsequent calls.

  • Default User Avatar

    can someone please explain how this works?I dont see what the first line is doing.

    why are you setting Singleton.a = this? And what is the purpose of this property a?

    Please explain, thanks.