這篇文章主要介紹如何使用js實現單鏈解決前端隊列問題,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
使用場景
比如前端需要處理什么隊列一類的業務
比如有人下單,需要彈出什么彈窗
首先先聲明一個類
接收一個 數組對象:items
class ChainQueue {
constructor(items) {
this.items = items || []
this.maxNum = 200
}
}為隊列添加數組隊列
// 添加數組隊列
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)
}
}刪除隊列,返回刪除的當前的項目
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
}
// 添加數組隊列
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)
}
}
// 刪除隊列,返回刪除的當前的項目
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())以上是“如何使用js實現單鏈解決前端隊列問題”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。