摘要:實現(xiàn)下面這道題中的函數(shù)這里等待了原型寫法維護一個數(shù)組,異步就函數(shù),不是異步就字符串寫法維護一個隊列數(shù)組,里面全部是構(gòu)造函數(shù),第一個參數(shù)代碼是否異步,第二個是比較數(shù)組每一個都是構(gòu)造函數(shù)有點浪費空間
實現(xiàn)下面這道題中的machine函數(shù)
function machine() { } machine("ygy").execute() // start ygy machine("ygy").do("eat").execute(); // start ygy // ygy eat machine("ygy").wait(5).do("eat").execute(); // start ygy // wait 5s(這里等待了5s) // ygy eat machine("ygy").waitFirst(5).do("eat").execute(); // wait 5s // start ygy // ygy eat原型寫法
維護一個數(shù)組,異步就push函數(shù),不是異步就push字符串
function machine(name) { if(!(this instanceof machine)){ return new machine(name) } this.name = name this.logs = [] this.logs.push(`start ${name}`) } machine.defer = function(time){ const times = time return function(){ console.log(`wait ${times}s`) return new Promise((resolve)=>{ setTimeout(()=>{resolve()},times*1000) }) } } machine.prototype.execute = async function(){ const logs = this.logs if(logs.length > 0){ for(let i=0; ies6寫法 維護一個queue隊列數(shù)組,里面全部是構(gòu)造函數(shù),第一個參數(shù)代碼是否異步,第二個是callback
function machine(name) { return new Action(name) } const defer = (time, callback) => { return new Promise((resolve) => { setTimeout(() => { resolve(callback()) }, time * 1000) }) } class QueueItem { constructor(defer, callback) { this.defer = defer; this.callback = callback; } } class Action { queue = [] constructor(name) { this.name = name; this.queue.push(new QueueItem(0, () => console.log(`start ${this.name}`))) } do(eat) { this.queue.push(new QueueItem(0, () => console.log(`${this.name} ${eat}`))) return this; } wait(time) { this.queue.push(new QueueItem(time, () => console.log(`wait ${time}s`))) return this; } waitFirst(time) { this.queue.unshift(new QueueItem(time, () => console.log(`wait ${time}s`))) return this; } async execute() { while(this.queue.length > 0) { const curItem = this.queue.shift(); if (!curItem.defer) { curItem.callback(); continue; } await defer(curItem.defer, curItem.callback) } } }比較數(shù)組每一個都是構(gòu)造函數(shù)有點浪費空間
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/110176.html
摘要:解決方案異或操作異或運算是對于二進制數(shù)字而言的,比如說一個有兩個二進制,如果兩個值不相同,則異或結(jié)果為。比如說,本質(zhì)上其實是和的每一對比特位執(zhí)行異或操作,等價于下面數(shù)字對應(yīng)的二進制數(shù)字對應(yīng)的二進制數(shù)字對應(yīng)的二進制因此的結(jié)果就為啦。 新年第一篇文章,先祝大家新年快樂!!那么接下來進入正文。 前言 前陣子突發(fā)奇想,突然開始刷leetcode。其中刷到了一道有意思的題目,發(fā)現(xiàn)這道題是當時秋招...
摘要:重溫一個面試題內(nèi)容數(shù)組內(nèi)容為數(shù)組內(nèi)容為個英文字母,使用兩個線程分別輸入兩個數(shù)組,打印內(nèi)容為這樣的規(guī)律提取一下核心內(nèi)容,去除次要內(nèi)容兩個線程需要交替執(zhí)行,打印數(shù)字的線程需要先執(zhí)行,數(shù)組打印完畢后線程需要結(jié)束。 一道多線程面試題引起的自我救贖 近日去一個知名互聯(lián)網(wǎng)企業(yè)參加面試,之前準備多多信心滿滿,但是面試一開始就是一道不起眼的編程題 數(shù)組A內(nèi)容為 1,2,3,4...52 ,數(shù)組B內(nèi)容...
摘要:下面我們來使用面向?qū)ο箢悎D這里就不再畫了首先面試題中所提到的我們都可以看成類,比如停車場是一個類吧,它里面的車位是一個類吧,攝像頭,屏幕。。。 以下是某場的一道面試題(大概): 1、一個停車場,車輛入場時,攝像頭記錄下車輛信息2、屏幕上顯示所接收的車輛的信息情況(車牌號)以及各層車位的車位余量3、停車場一共四層車位,其中的三層都為普通車位,還有一層為特殊車位(體現(xiàn)在停車計費價格上面的不...
閱讀 796·2021-10-14 09:42
閱讀 2024·2021-09-22 15:04
閱讀 1665·2019-08-30 12:44
閱讀 2211·2019-08-29 13:29
閱讀 2786·2019-08-29 12:51
閱讀 604·2019-08-26 18:18
閱讀 777·2019-08-26 13:43
閱讀 2872·2019-08-26 13:38