javascript singleton question

前端 未结 10 2039
闹比i
闹比i 2020-12-12 20:08

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

相关标签:
10条回答
  • 2020-12-12 20:13

    I've wondered about this too, but just defining an object with functions in it seems reasonable to me. No sense creating a constructor that nobody's ever supposed to call, to create an object with no prototype, when you can just define the object directly.

    On the other hand, if you want your singleton to be an instance of some existing "class" -- that is, you want it to have some other object as its prototype -- then you do need to use a constructor function, so that you can set its prototype property before calling it.

    0 讨论(0)
  • 2020-12-12 20:13

    The latter code box shows what I've seen JS devs call their version of OO design in Javascript.

    Singetons are meant to be singular objects that can't be constructed (except, I suppose, in the initial definition. You have one, global instance of a singleton.

    0 讨论(0)
  • 2020-12-12 20:16

    The point of using the "pseudo constructor" is that it creates a new variable scope. You can declare local variables inside the function that are available inside any nested functions but not from the global scope.

    There are actually two ways of doing it. You can call the function with new like in your example, or just call the function directly. There are slight differences in how you would write the code, but they are essentially equivalent.

    Your second example could be written like this:

    var singleton = new function () {
        var privateVariable = 42; // This can be accessed by dothis and dothat
    
        this.dothis = function () {
            return privateVariable;
        };
    
        this.dothat = function () {};
    }; // Parentheses are allowed, but not necessary unless you are passing parameters
    

    or

    var singleton = (function () {
        var privateVariable = 42; // This can be accessed by dothis and dothat
    
        return {
            dothis: function () {
                return privateVariable;
            },
    
            dothat: function () {}
        };
    })(); // Parentheses are required here since we are calling the function
    

    You could also pass arguments to either function (you would need to add parentheses to the first example).

    0 讨论(0)
  • 2020-12-12 20:20

    The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. 1

    (function (global) {
    
         var singleton;
    
         function Singleton () {
             // singleton does have a constructor that should only be used once    
             this.foo = "bar";
             delete Singleton; // disappear the constructor if you want
         }
    
         global.singleton = function () {
             return singleton || (singleton = new Singleton());
         };
    
    })(window);
    
    var s = singleton();
    console.log(s.foo);
    
    var y = singleton();
    y.foo = "foo";
    console.log(s.foo);
    

    You don't just declare the singleton as an object because that instantiates it, it doesn't declare it. It also doesn't provide a mechanism for code that doesn't know about a previous reference to the singleton to retrieve it. The singleton is not the object/class that is returned by the singleton, it's a structure. This is similar to how closured variables are not closures, the function scope providing the closure is the closure.

    0 讨论(0)
  • 2020-12-12 20:21

    Crockford (seems to) agree that the object literal is all you need for a singleton in JavaScript:

    http://webcache.googleusercontent.com/search?q=cache:-j5RwC92YU8J:www.crockford.com/codecamp/The%2520Good%2520Parts%2520ppt/5%2520functional.ppt+singleton+site:www.crockford.com&cd=1&hl=en&ct=clnk

    0 讨论(0)
  • 2020-12-12 20:22

    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.

    Edit: 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
    }
    

    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.

    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
    

    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
    
    0 讨论(0)
提交回复
热议问题