search

Home  >  Q&A  >  body text

javascript - Doubts about singleton mode in js

The following code

function Universe() {

    // 缓存的实例
    var instance = this;

    // 其它内容
    this.start_time = 0;
    this.bang = "Big";

    // 重写构造函数
    Universe = function () {
        return instance;
    };
}

// 测试
var uni = new Universe();
var uni2 = new Universe();
uni.bang = "123";
console.log(uni === uni2); // true
console.log(uni2.bang); // 123

Click to view the original text

question:

new Universe()的过程是:
var o = {};
Universe.call(o);//这一步是将Universe中this对应的属性赋值给o;
o.__proto__ = Universe.prototype;//将Universe原型上的方法赋值到o的__proto__属性上;

Then var instance = this;, does this refer to a different object o? So after rewriting the constructor, isn't the returned instance a different object? Why are they equal in the end

怪我咯怪我咯2749 days ago732

reply all(6)I'll reply

  • 某草草

    某草草2017-06-30 10:00:37

    Because after rewriting the constructor, an object is returned. This object will overwrite the object you generated with the new constructor. I don’t know if I made it clear

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-30 10:00:37

    @mpccc is right.

    If the constructor returns an object, then the new object will be the object.

    You can take a look at the constructor section in Secret Garden

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-30 10:00:37

    I’m a newbie too, so I’ll try to answer it, don’t blame me if I’m wrong

    First, does this refer to different objects? When the constructor is called for the first time, an empty object is generated and this inside the function points to the empty object. Then the code is executed, and finally the object is returned, which is
    uni .

    In the second call, due to the rewriting of the first function, a closure was generated. The internal instance of this closure just pointed to the object generated in the first call

    uni. When the second call When you execute new Universe(), you are executing a closure, which will also generate an empty object, but that object does not use it. Instead, it directly returns the instance inside the closure, which is uni.

    So

    uni2 === uni.

    reply
    0
  • 迷茫

    迷茫2017-06-30 10:00:37

    Another question, writing a singleton pattern like this is a bit redundant. To create a unique objectyou don’t have to create a constructor

    var single = function(fn){ 
     var instance; 
     return function(){ 
         return instance || (instance = fn .apply(this, arguments)); 
     } 
    }; 

    reply
    0
  • ringa_lee

    ringa_lee2017-06-30 10:00:37

    Didn’t you write the comments and rewrite the constructor? After you create new once, you will no longer have the line of code var instance = this;, and the instance will naturally remain unchanged

    //简单打印一下就知道了
    console.log(Universe)
    var uni = new Universe()
    console.log(Universe)

    reply
    0
  • 阿神

    阿神2017-06-30 10:00:37

    When new Universe() is executed for the first time, a new this is indeed generated, and the Universe constructor is rewritten. When this new Universe() is called again later, it will only return instance, no new objects will be generated.

    reply
    0
  • Cancelreply