中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

JavaScript 設(shè)計(jì)模式學(xué)習(xí) Singleton

復(fù)制代碼 代碼如下:
/* Basic Singleton. */
var Singleton = {
attribute1: true,
attribute2: 10,
method1: function() {
},
method2: function(arg) {
}
};
單件模式最主要的用途之一就是命名空間:
/* GiantCorp namespace. */
var GiantCorp = {};
GiantCorp.Common = {
// A singleton with common methods used by all objects and modules.
};
GiantCorp.ErrorCodes = {
// An object literal used to store data.
};
GiantCorp.PageHandler = {
// A singleton with page specific methods and attributes.
};
利用閉包在單件模式中實(shí)現(xiàn)私有方法和私有變量:
GiantCorp.DataParser = (function() {
// Private attributes.
var whitespaceRegex = //s+/;
// Private methods.
function stripWhitespace(str) {
return str.replace(whitespaceRegex, '');
}
function stringSplit(str, delimiter) {
return str.split(delimiter);
}
// Everything returned in the object literal is public, but can access the
// members in the closure created above.
return {
// Public method.
stringToArray: function(str, delimiter, stripWS) {
if(stripWS) {
str = stripWhitespace(str);
}
var outputArray = stringSplit(str, delimiter);
return outputArray;
}
};
})(); // Invoke the function and assign the returned object literal to
// GiantCorp.DataParser.
實(shí)現(xiàn)Lazy Instantiation 單件模式:
MyNamespace.Singleton = (function() {
var uniqueInstance; // Private attribute that holds the single instance.
function constructor() { // All of the normal singleton code goes here.
...
}
return {
getInstance: function() {
if(!uniqueInstance) { // Instantiate only if the instance doesn't exist.
uniqueInstance = constructor();
}
return uniqueInstance;
}
}
})();
MyNamespace.Singleton.getInstance().publicMethod1();

JavaScript技術(shù)JavaScript 設(shè)計(jì)模式學(xué)習(xí) Singleton,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 成人精品 | 日韩中文在线视频 | 国产视频一二三区 | 国产免费一区二区三区网站免费 | 日本三级电影免费 | 久热m3u8| 国产精品日韩在线观看 | 国产女人第一次做爰毛片 | 欧美日韩黄色一级片 | 色综合色综合色综合 | 欧美综合一区 | 999久久久国产精品 欧美成人h版在线观看 | 国产成人综合网 | 亚洲欧美视频 | 国产一区二区在线视频 | 91精品国产乱码久久久久久久久 | 97超级碰碰 | 91免费观看视频 | 国产综合精品一区二区三区 | 蜜桃一区 | 2021狠狠干 | 久久久夜夜夜 | 91色视频在线观看 | a视频在线观看 | 久久9视频| 亚洲欧美成人 | 国产精品久久久久久久久久三级 | h漫在线观看 | 中文一区二区 | 欧美一级黑人aaaaaaa做受 | 一级毛片免费 | 国产乱码一二三区精品 | 国产一区二区三区四区 | 国产视频中文字幕在线观看 | 人人草人人干 | 免费在线成人网 | 欧美在线观看一区 | 精品一区电影 | 国产视频二区 | 99精品一区二区 | 天天综合网天天综合 |