Javascript:最好的Singleton模式
可能重复:
最简单/最干净的方式来实现JavaScript中的单身人士?
我使用这种模式的单身人士,例如单身人士是PlanetEarth:
var NAMESPACE = function () { var privateFunction1 = function () { privateFunction2(); }; var privateFunction2 = function () { alert('I\'m private!'); }; var Constructors = {}; Constructors.PlanetEarth = function () { privateFunction1(); privateFunction2(); }; Constructors.PlanetEarth.prototype = { someMethod: function () { if (console && console.log) { console.log('some method'); } } }; Constructors.Person = function (name, address) { this.name = name; this.address = address; }; Constructors.Person.prototype = { walk: function () { alert('STOMP!'); } }; return { Person: Constructors.Person, // there can be many PlanetEarth: new Constructors.PlanetEarth() // there can only be one! }; }();
由于PlanetEarth的构造函数保持私有,所以只能有一个。
现在有人告诉我,这种自制的东西不是最好的,主要是因为我没有学历教育,而且我倾向于用愚蠢的方式解决问题。 你会提出什么样的更好的select,我的方法, 更好的定义是风格上更好和/或更强大 ?
find最佳解决scheme: http : //code.google.com/p/jslibs/wiki/JavascriptTips#Singleton_pattern
function MySingletonClass () { if (arguments.callee._singletonInstance) { return arguments.callee._singletonInstance; } arguments.callee._singletonInstance = this; this.Foo = function () { // ... }; } var a = new MySingletonClass(); var b = MySingletonClass(); console.log( a === b ); // prints: true
对于那些想要严格版本的人:
(function (global) { "use strict"; var MySingletonClass = function () { if (MySingletonClass.prototype._singletonInstance) { return MySingletonClass.prototype._singletonInstance; } MySingletonClass.prototype._singletonInstance = this; this.Foo = function() { // ... }; }; var a = new MySingletonClass(); var b = MySingletonClass(); global.result = a === b; } (window)); console.log(result);
为什么使用一个构造函数和原型为一个单一的对象?
以上相当于:
var earth= { someMethod: function () { if (console && console.log) console.log('some method'); } }; privateFunction1(); privateFunction2(); return { Person: Constructors.Person, PlanetEarth: earth };
如果你需要一个types声明并使用一个variables来访问这个单例实例,下面的代码可能会有所帮助。 我喜欢这个符号,因为代码是自我引导的。
function SingletonClass(){ if ( arguments.callee.instance ) return arguments.callee.instance; arguments.callee.instance = this; } SingletonClass.getInstance = function() { var singletonClass = new SingletonClass(); return singletonClass; };
要访问单身人士,你会的
var singleTon = SingletonClass.getInstance();
function SingletonClass() { // demo variable var names = []; // instance of the singleton this.singletonInstance = null; // Get the instance of the SingletonClass // If there is no instance in this.singletonInstance, instanciate one var getInstance = function() { if (!this.singletonInstance) { // create a instance this.singletonInstance = createInstance(); } // return the instance of the singletonClass return this.singletonInstance; } // function for the creation of the SingletonClass class var createInstance = function() { // public methodes return { add : function(name) { names.push(name); }, names : function() { return names; } } } // wen constructed the getInstance is automaticly called and return the SingletonClass instance return getInstance(); } var obj1 = new SingletonClass(); obj1.add("Jim"); console.log(obj1.names()); // prints: ["Jim"] var obj2 = new SingletonClass(); obj2.add("Ralph"); console.log(obj1.names()); // Ralph is added to the singleton instance and there for also acceseble by obj1 // prints: ["Jim", "Ralph"] console.log(obj2.names()); // prints: ["Jim", "Ralph"] obj1.add("Bart"); console.log(obj2.names()); // prints: ["Jim", "Ralph", "Bart"]