JS實(shí)現(xiàn)單例模式的N種方案
JS實(shí)現(xiàn)單例模式的多種方案 ,本文稍加總結(jié),列出了6種方式與大家分享,大體上將內(nèi)容分為了ES5(Function)與ES6(Class)實(shí)現(xiàn)兩種部分
JS實(shí)現(xiàn)單例模式的多種方案
今天在復(fù)習(xí)設(shè)計(jì)模式中的-創(chuàng)建型模式,發(fā)現(xiàn)JS實(shí)現(xiàn)單例模式的方案有很多種,稍加總結(jié)了一下,列出了如下的6種方式與大家分享
大體上將內(nèi)容分為了ES5(Function)與ES6(Class)實(shí)現(xiàn)兩種部分
單例模式的概念
- 一個(gè)實(shí)例只生產(chǎn)一次
- 保證一個(gè)類(lèi)僅有一個(gè)實(shí)例,并提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)
方式1
利用instanceof判斷是否使用new關(guān)鍵字調(diào)用函數(shù)進(jìn)行對(duì)象的實(shí)例化
function User() {
if (!(this instanceof User)) {
return
}
if (!User._instance) {
this.name = '無(wú)名'
User._instance = this
}
return User._instance
}
const u1 = new User()
const u2 = new User()
console.log(u1===u2);// true方式2
在函數(shù)上直接添加方法屬性調(diào)用生成實(shí)例
function User(){
this.name = '無(wú)名'
}
User.getInstance = function(){
if(!User._instance){
User._instance = new User()
}
return User._instance
}
const u1 = User.getInstance()
const u2 = User.getInstance()
console.log(u1===u2);方式3
使用閉包,改進(jìn)方式2
function User() {
this.name = '無(wú)名'
}
User.getInstance = (function () {
var instance
return function () {
if (!instance) {
instance = new User()
}
return instance
}
})()
const u1 = User.getInstance()
const u2 = User.getInstance()
console.log(u1 === u2);方式4
使用包裝對(duì)象結(jié)合閉包的形式實(shí)現(xiàn)
const User = (function () {
function _user() {
this.name = 'xm'
}
return function () {
if (!_user.instance) {
_user.instance = new _user()
}
return _user.instance
}
})()
const u1 = new User()
const u2 = new User()
console.log(u1 === u2); // true當(dāng)然這里可以將閉包部分的代碼單獨(dú)封裝為一個(gè)函數(shù)
在頻繁使用到單例的情況下,推薦使用類(lèi)似此方法的方案,當(dāng)然內(nèi)部實(shí)現(xiàn)可以采用上述任意一種
function SingleWrapper(cons) {
// 排除非函數(shù)與箭頭函數(shù)
if (!(cons instanceof Function) || !cons.prototype) {
throw new Error('不是合法的構(gòu)造函數(shù)')
}
var instance
return function () {
if (!instance) {
instance = new cons()
}
return instance
}
}
function User(){
this.name = 'xm'
}
const SingleUser = SingleWrapper(User)
const u1 = new SingleUser()
const u2 = new SingleUser()
console.log(u1 === u2);方式5
在構(gòu)造函數(shù)中利用new.target判斷是否使用new關(guān)鍵字
class User{
constructor(){
if(new.target !== User){
return
}
if(!User._instance){
this.name = 'xm'
User._instance = this
}
return User._instance
}
}
const u1 = new User()
const u2 = new User()
console.log(u1 === u2);方式6
使用static靜態(tài)方法
class User {
constructor() {
this.name = 'xm'
}
static getInstance() {
if (!User._instance) {
User._instance = new User()
}
return User._instance
}
}
const u1 = User.getInstance()
const u2 = User.getInstance()
console.log(u1 === u2);到此這篇關(guān)于JS實(shí)現(xiàn)單例模式的N種方案的文章就介紹到這了,更多相關(guān)js單例模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS隨拖拽速度設(shè)置傾斜角度的實(shí)現(xiàn)代碼
這篇文章主要給大家介紹了JS如何隨拖拽速度設(shè)置傾斜角度,文中有詳細(xì)的代碼講解,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以自己動(dòng)手嘗試一下2023-09-09
JS庫(kù)particles.js創(chuàng)建超炫背景粒子插件(附源碼下載)
particles.js用于創(chuàng)建粒子的輕量級(jí) JavaScript 庫(kù)。使用方法非常簡(jiǎn)單,代碼也很容易實(shí)現(xiàn),下面通過(guò)本文給大家分享JS庫(kù)particles.js創(chuàng)建超炫背景粒子插件附源碼下載,需要的朋友參考下吧2017-09-09
配置eslint規(guī)范項(xiàng)目代碼風(fēng)格
這篇文章主要介紹了配置eslint規(guī)范項(xiàng)目代碼風(fēng)格,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
微信公眾號(hào)錄音文件的播放與保存(amr文件轉(zhuǎn)mp3)
本文主要介紹了微信公眾號(hào)錄音文件的播放與保存(amr文件轉(zhuǎn)mp3),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
詳解Webpack抽離第三方類(lèi)庫(kù)以及common解決方案
這篇文章主要介紹了詳解Webpack抽離第三方類(lèi)庫(kù)以及common解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
js點(diǎn)擊圖片實(shí)現(xiàn)查看大圖簡(jiǎn)單方法
今天開(kāi)發(fā)的時(shí)候,遇到要點(diǎn)擊縮略圖之后顯示圖片的大圖查看,所以本文給大家分享下,這篇文章主要給大家介紹了關(guān)于js點(diǎn)擊圖片實(shí)現(xiàn)查看大圖的簡(jiǎn)單方法,需要的朋友可以參考下2023-06-06

