javascript单例问题 [英] javascript singleton question

查看:172
本文介绍了javascript单例问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在javascript中讨论了单例设计的几个主题。我是100%新设计模式的东西,但正如我所看到的,因为根据定义,Singleton不需要实例化,概念上如果它不被实例化,在我看来它不必被视为从蓝图(类)创建的常规对象。所以我的奇怪是为什么不仅仅考虑一个单独的东西,就像静态可用的东西包含在某种范围内而应该是全部。

I just read a few threads on the discussion of singleton design in javascript. I'm 100% new to the Design Pattern stuff but as I see since a Singleton by definition won't have the need to be instantiated, conceptually if it's not to be instantiated, in my opinion it doesn't have to be treated like conventional objects which are created from a blueprint(classes). So my wonder is why not just think of a singleton just as something statically available that is wrapped in some sort of scope and that should be all.

从我看到的线程,他们中的大多数都是单身,但传统的javascript

From the threads I saw, most of them make a singleton though traditional javascript

new function(){} 

然后创建一个伪构造函数。

followed by making a pseudo constructor.

我只是认为对象文字已经足够了够了:

Well I just think an object literal is enough enough:

var singleton = {
   dothis: function(){},
   dothat: function(){}
}

对吗?或者任何人都有更好的见解?

right? Or anybody got better insights?

[更新]:我的观点再次表明,为什么人们只是使用更简单的方法在javascript中制作单身,就像我在第二个片段中所展示的那样,如果有绝对的话理由请告诉我。我通常害怕这种情况,我简化了很多事情:D

[update] : Again my point is why don't people just use a simpler way to make singletons in javascript as I showed in the second snippet, if there's an absolute reason please tell me. I'm usually afraid of this kind of situation that I simplify things to much :D

推荐答案

我同意你的意见,最简单方法是使用对象文字,但如果你想要私有成员,你可以实现利用关闭

I agree with you, the simplest way is to use a object literal, but if you want private members, you could implement taking advantage of closures:

var myInstance = (function() {
  var privateVar;

  function privateMethod () {
    // ...
  }

  return { // public interface
    publicMethod1: function () {
      // private members can be accessed here
    },
    publicMethod2: function () {
      // ...
    }
  };
})();

关于新函数(){} 构造,它将简单地使用匿名函数作为 构造函数函数 ,该函数中的上下文将是一个将返回的新对象。

About the new function(){} construct, it will simply use an anonymous function as a constructor function, the context inside that function will be a new object that will be returned.

编辑:响应 @ J5的评论,这很简单,实际上我认为这可以是使用懒惰函数定义模式的一个很好的例子:

In response to the @J5's comment, that is simple to do, actually I think that this can be a nice example for using a Lazy Function Definition pattern:

function singleton() {
  var instance = (function() {
    var privateVar;

    function privateMethod () {
      // ...
    }

    return { // public interface
      publicMethod1: function () {
          // private members can be accessed here
       },
      publicMethod2: function () {
        // ...
      }
    };
  })();

  singleton = function () { // re-define the function for subsequent calls
    return instance;
  };

  return singleton(); // call the new function
}

第一次调用函数时,我创建对象实例,并将 singleton 重新分配给一个新函数,该函数在其闭包中有该对象实例。

When the function is called the first time, I make the object instance, and reassign singleton to a new function which has that object instance in it's closure.

之前第一次调用结束时,我执行重新定义的单例函数,该函数将返回创建的实例。

Before the end of the first time call I execute the re-defined singleton function that will return the created instance.

调用 singleton 函数后,将只返回存储在其闭包中的实例,因为新函数是一个将被执行。

Following calls to the singleton function will simply return the instance that is stored in it's closure, because the new function is the one that will be executed.

你可以通过比较返回的对象来证明:

You can prove that by comparing the object returned:

singleton() == singleton(); // true

== 运算符只有当两个操作数的对象引用相同时,对象才会返回 true ,即使对象相同但它们是两个,它也将返回false不同的实例:

The == operator for objects will return true only if the object reference of both operands is the same, it will return false even if the objects are identical but they are two different instances:

({}) == ({}); // false
new Object() == new Object(); // false

这篇关于javascript单例问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆