function - Understanding singleton object in Javascript -


i'm having trouble understanding happens when instance calls new universe(). doesn't return "undefined"?

function universe() {   var instance;    universe = function universe() {     return instance;   }    universe.prototype = this;   instance = new universe();   instance.constructor = universe;   instance.start_time = 0;   instance.bang = "big";   return instance; } 

it wont return undefined instead of return object.see in alert box or in console;

 function universe() {           var instance;            universe = function universe() {             return instance;           }            universe.prototype = this;           instance = new universe();           alert("ins"+instance);//or        console.log(instance);           instance.constructor = universe;           instance.start_time = 0;           instance.bang = "big";           return instance;         }          alert(universe());//or console.log(universe()); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -