使用js實現(xiàn)單鏈解決前端隊列問題的方法
更新時間:2020年02月03日 10:24:04 作者:starkwang
這篇文章主要介紹了使用js實現(xiàn)單鏈解決前端隊列問題的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
使用場景
- 比如前端需要處理什么隊列一類的業(yè)務(wù)
- 比如有人下單,需要彈出什么彈窗
首先先聲明一個類
接收一個 數(shù)組對象:items
class ChainQueue {
constructor(items) {
this.items = items || []
this.maxNum = 200
}
}
為隊列添加數(shù)組隊列
// 添加數(shù)組隊列
entryArrQueue(node) {
Array.isArray(node)
node.map(item => this.items.push(item))
}
為當(dāng)前隊列添加單個對象
// 添加隊列
entryQueue(node) {
if (this.items.length > this.maxNum) {
return
}
if (Array.isArray(node)) {
node.map(item => this.items.push(item))
} else {
this.items.push(node)
}
}
刪除隊列,返回刪除的當(dāng)前的項目
deleteQueue(func = () => {}) {
assert(isFunc(func), `${func} is not function`)
func(this.items.shift())
}
返回隊列的第一個
front() {
return this.items[0]
}
清除隊列
clear() {
this.items = []
}
所有代碼
const isFunc = v => typeof v === 'function'
const assert = (condition, msg) => {
if (!condition) throw new Error(`[dashboard]${msg}`)
}
class ChainQueue {
constructor(items) {
this.items = items || []
this.maxNum = 200
}
// 添加數(shù)組隊列
entryArrQueue(node) {
Array.isArray(node)
node.map(item => this.items.push(item))
}
// 添加隊列
entryQueue(node) {
if (this.items.length > this.maxNum) {
return
}
if (Array.isArray(node)) {
node.map(item => this.items.push(item))
} else {
this.items.push(node)
}
}
// 刪除隊列,返回刪除的當(dāng)前的項目
deleteQueue(func = () => {}) {
assert(isFunc(func), `${func} is not function`)
func(this.items.shift())
}
// 返回隊列的第一個
front() {
return this.items[0]
}
// 清除隊列
clear() {
this.items = []
}
get size() {
return this.items.length
}
get isEmpty() {
return !this.items.length
}
print() {
console.log(this.items.toString())
console.log(this.items)
}
result() {
return this.items
}
}
module.exports = ChainQueue
// export default ChainQueue
// export default (ChainQueue = new ChainQueue())
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析Javascript中雙等號(==)隱性轉(zhuǎn)換機(jī)制
這篇文章給大家詳細(xì)介紹了javascript中雙等號(==)隱性轉(zhuǎn)換機(jī)制,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-10-10
JavaScript自定義Webpack配置實現(xiàn)流程介紹
本系列主要整理前端面試中需要掌握的知識點。本節(jié)介紹webpack如何優(yōu)化前端性能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-10-10
JS檢測window.open打開的窗口是否關(guān)閉
在開發(fā)中遇到需要在打開窗口的同時給父窗口添加遮罩防止用戶誤操作,而在窗口關(guān)閉時需要去掉父窗口的遮罩以便用戶操作。所以可以利用setInterval()來周期性的檢測打開的窗口是否關(guān)閉2017-06-06

