js單例模式詳解實(shí)例
什么是單例?
單例要求一個(gè)類有且只有一個(gè)實(shí)例,提供一個(gè)全局的訪問點(diǎn)。因此它要繞過常規(guī)的控制器,使其只能有一個(gè)實(shí)例,供使用者使用,而使用著不關(guān)心有幾個(gè)實(shí)例,因此這是設(shè)計(jì)者的責(zé)任
In JavaScript, Singletons serve as a shared resource namespace which isolate implementation code from the global namespace so as to provide a single point of access for functions.
在javascript中,單例被當(dāng)做一個(gè)全局的命名空間,提供一個(gè)訪問該對(duì)象的一個(gè)點(diǎn)。
使用場(chǎng)景
In practice, the Singleton pattern is useful when exactly one object is needed to coordinate others across a system.
單例比較適用于一個(gè)對(duì)象和其他系統(tǒng)進(jìn)行交互。
類比
單例有點(diǎn)類似于一個(gè)小組的小組長,在一段時(shí)間內(nèi)只有一個(gè)小組長,有小組長來指定組員的工作,分配和協(xié)調(diào)和組員的工作。
實(shí)例1:這個(gè)是最簡(jiǎn)單的單例,通過key,value的形式存儲(chǔ)屬性和方法
var A = {
xx:3,
yy:4,
B:function(el){
},
C:function(el){
},
D:function(el){
},
E:function(el){
}
}
實(shí)例2:首先創(chuàng)建一個(gè)實(shí)例的引用,然后判斷這個(gè)實(shí)例是否存在,如果不存在那么就創(chuàng)建,存在的話,就直接返回,保證有且只有一個(gè)。
var mySingleton = (function () {
// Instance 存儲(chǔ)一個(gè)單例實(shí)例的引用
var instance;
function init() {
// Singleton
// 私有的方法和變量
function privateMethod(){
console.log( "I am private" );
}
var privateVariable = "Im also private";
return {
// 共有的方法和變量
publicMethod: function () {
console.log( "The public can see me!" );
},
publicProperty: "I am also public"
};
};
return {
// 如果實(shí)例不存在,那么創(chuàng)建一個(gè)
getInstance: function () {
if ( !instance ) {
instance = init();
}
return instance;
}
};
})();
var singleA = mySingleton;
var singleB = mySingleton;
console.log( singleA === singleB ); // true
實(shí)例3:
var SingletonTester = (function () {
// options: an object containing configuration options for the singleton
// e.g var options = { name: "test", pointX: 5};
function Singleton( options ) {
// set options to the options supplied
// or an empty object if none are provided
options = options || {};
// set some properties for our singleton
this.name = "SingletonTester";
this.pointX = options.pointX || 6;
this.pointY = options.pointY || ;
}
// our instance holder
var instance;
// an emulation of static variables and methods
var _static = {
name: "SingletonTester",
// Method for getting an instance. It returns
// a singleton instance of a singleton object
getInstance: function( options ) {
if( instance === undefined ) {
instance = new Singleton( options );
}
return instance;
}
};
return _static;
})();
var singletonTest = SingletonTester.getInstance({
pointX: 5
});
// Log the output of pointX just to verify it is correct
// Outputs: 5
console.log( singletonTest.pointX );
相關(guān)文章
對(duì)Js OOP編程 創(chuàng)建對(duì)象的一些全面理解
下面小編就為大家?guī)硪黄獙?duì)Js OOP編程 創(chuàng)建對(duì)象的一些全面理解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07js 使用form表單select類實(shí)現(xiàn)級(jí)聯(lián)菜單效果
本文介紹javascript使用html中form表單中的select類實(shí)現(xiàn)級(jí)聯(lián)菜單效果,需要的朋友可以了解下2012-12-12JavaScript與DOM組合動(dòng)態(tài)創(chuàng)建表格實(shí)例
這篇文章簡(jiǎn)單介紹了DOM 1.0一些基本而強(qiáng)大的方法以及如何在JavaScript中使用它們;你可以學(xué)到如何動(dòng)態(tài)地創(chuàng)建、獲取、控制和刪除HTML元素2012-12-12javascript的數(shù)據(jù)類型、字面量、變量介紹
javascript的數(shù)據(jù)類型、字面量、變量介紹,學(xué)習(xí)js的朋友可以參考下2012-05-05js中的setInterval和setTimeout使用實(shí)例
這篇文章主要介紹了javascript中的兩個(gè)定時(shí)執(zhí)行函數(shù)setInterval和setTimeout的用法,需要的朋友可以參考下2014-05-05Javascript學(xué)習(xí)筆記之?dāng)?shù)組的構(gòu)造函數(shù)
這篇文章主要介紹了Javascript數(shù)組的構(gòu)造函數(shù)及常見的操作,講解的十分詳細(xì),這里推薦給大家2014-11-11