簡體   English   中英

了解Javascript中的單例對象

[英]Understanding singleton object in Javascript

我無法理解實例調用new Universe()時會發生什么。 它不只是返回“ 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;
}

它不會返回undefined而不是它將返回object。請參見警報框或控制台;

 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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM