问题描述
可能的重复:
在Javascript中实现Singleton? p>
我正在为单胎使用这种模式,在示例中,单身是行星:
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! }; }();
因为行星的构造变量仍然是私人的,所以只有一个.
现在,有些东西告诉我,这种自熟的事情不是最好的做,主要是因为我没有学术教育,我倾向于以愚蠢的方式解决问题.您将如何提出更好的替代方法,其中更好的在风格上定义为 ,和/或更强大的?
推荐答案
(1)更新2019:ES7版本
class Singleton { static instance; constructor() { if (instance) { return instance; } this.instance = this; } foo() { // ... } } console.log(new Singleton() === new Singleton());
(2)ES6版本
class Singleton { constructor() { const instance = this.constructor.instance; if (instance) { return instance; } this.constructor.instance = this; } foo() { // ... } } console.log(new Singleton() === new Singleton());
找到最佳解决方案: http://code.google.com/p/p/p/jslibs/jslibs/wiki/wiki/javascripttips@spriptttipt-siptlettneletn_pattern,patter.singletn.patern,patr.
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 };
其他推荐答案
类型声明并使用变量访问Singleton实例,以下代码可能会有所帮助.我喜欢这种符号,因为代码几乎没有自我指导.
function SingletonClass(){ if ( arguments.callee.instance ) return arguments.callee.instance; arguments.callee.instance = this; } SingletonClass.getInstance = function() { var singletonClass = new SingletonClass(); return singletonClass; };
要访问Singleton,您将
var singleTon = SingletonClass.getInstance();
问题描述
Possible Duplicate:
Simplest/Cleanest way to implement singleton in JavaScript?
I'm using this pattern for singletons, in the example the singleton is 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! }; }();
Since PlanetEarth's constructor remains private, there can only be one.
Now, something tells me that this self-cooked thing isn't the best one can do, mostly because I don't have an academic education and I tend to solve problems in stupid ways. What would you propose as a better alternative my method, where better is defined as stylistically better and/or more powerful?
推荐答案
(1) UPDATE 2019: ES7 Version
class Singleton { static instance; constructor() { if (instance) { return instance; } this.instance = this; } foo() { // ... } } console.log(new Singleton() === new Singleton());
(2) ES6 Version
class Singleton { constructor() { const instance = this.constructor.instance; if (instance) { return instance; } this.constructor.instance = this; } foo() { // ... } } console.log(new Singleton() === new Singleton());
Best solution found: 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
For those who want the strict version:
(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);
其他推荐答案
Why use a constructor and prototyping for a single object?
The above is equivalent to:
var earth= { someMethod: function () { if (console && console.log) console.log('some method'); } }; privateFunction1(); privateFunction2(); return { Person: Constructors.Person, PlanetEarth: earth };
其他推荐答案
Extending the above post by Tom, if you need a class type declaration and access the singleton instance using a variable, the code below might be of help. I like this notation as the code is little self guiding.
function SingletonClass(){ if ( arguments.callee.instance ) return arguments.callee.instance; arguments.callee.instance = this; } SingletonClass.getInstance = function() { var singletonClass = new SingletonClass(); return singletonClass; };
To access the singleton, you would
var singleTon = SingletonClass.getInstance();