0

I'm having trouble understanding what happens when the instance calls new Universe(). Doesn't it just 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;
}
1

1 Answer 1

1

it wont return undefined instead of that it will 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());
Sign up to request clarification or add additional context in comments.

2 Comments

But, what about the instance in the newly declared Universe function. It does not define a value for instance, so why doesn't it return undefined when it invokes new Universe()?
@mike initially it will be undefined only but later when you will use new Universe() it means you are asking for an object therefore the value returned will be converted into object hence you will not have undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.