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è)訪(fǎng)問(wèn)它的全局訪(fǎng)問(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)文章
理解 javascript 中的函數(shù)表達(dá)式與函數(shù)聲明
這篇文章主要介紹了理解 javascript 中的函數(shù)表達(dá)式與函數(shù)聲明,需要的朋友可以參考下2017-07-07
利用ES6實(shí)現(xiàn)單例模式及其應(yīng)用詳解
單例是在程序設(shè)計(jì)非?;A(chǔ)的東西,這篇文章主要給大家介紹了關(guān)于利用ES6實(shí)現(xiàn)單例模式及其應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
微信小程序報(bào)錯(cuò): thirdScriptError的錯(cuò)誤問(wèn)題
這篇文章主要介紹了微信小程序報(bào)錯(cuò): thirdScriptError,本文給大家分享解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
JavaScript表單驗(yàn)證實(shí)例之驗(yàn)證表單項(xiàng)是否為空
表單驗(yàn)證幾乎在每個(gè)需要注冊(cè)或者是登錄的網(wǎng)站都是必不可少,下面通過(guò)本篇文章給大家介紹JavaScript表單驗(yàn)證實(shí)例之驗(yàn)證表單項(xiàng)是否為空,涉及到j(luò)s表單驗(yàn)證實(shí)例相關(guān)知識(shí),對(duì)js表單驗(yàn)證實(shí)例代碼需要的朋友一起學(xué)習(xí)吧2016-01-01

