成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

前端筆試之手寫(xiě)代碼

zhichangterry / 3012人閱讀

摘要:扁平化嵌套數(shù)組實(shí)現(xiàn)描述將嵌套多層的數(shù)組展開(kāi)平鋪成只有一層的數(shù)組。方法一知識(shí)點(diǎn)方法二知識(shí)點(diǎn)方法三知識(shí)點(diǎn)展開(kāi)語(yǔ)法其它方法數(shù)組去重描述將數(shù)組中重復(fù)的元素過(guò)濾掉。對(duì)于函數(shù)防抖,門(mén)外有人頻繁敲門(mén),門(mén)衛(wèi)按最后一次敲門(mén)來(lái)決定是否開(kāi)門(mén)。知識(shí)點(diǎn)發(fā)布訂閱模式

1. 扁平化嵌套數(shù)組/flat實(shí)現(xiàn)

描述:將嵌套多層的數(shù)組展開(kāi)平鋪成只有一層的數(shù)組。

let array = [1, [1, 2, 3], [1, [2, {}]] ]
handle(array) // [1, 1, 2, 3, 1, 2, {}]

方法一

const handle = array => JSON.parse(`[${JSON.stringify(array).replace(/[|]/g,"")}]`)
handle(array)   // [ 1, 1, 2, 3, 1, 2, {} ]

知識(shí)點(diǎn)JSON.parse()/JSON.stringify()String.prototype.replace()

方法二

const handle = array => array.reduce((accumulator, currentValue) => accumulator.concat(Array.isArray(currentValue) ? handle(currentValue): currentValue), [])
handle(array)   // [ 1, 1, 2, 3, 1, 2, {} ]

知識(shí)點(diǎn)Array.prototype.reduce()、Array.prototype.concat()

方法三

const handle = array => {
    while(array.some(item => Array.isArray(item))) {
        array = [].concat(...array)
    }
    return array
}
handle(array)   // [ 1, 1, 2, 3, 1, 2, {} ]

知識(shí)點(diǎn)while、Array.prototype.some()、展開(kāi)語(yǔ)法(Spread syntax)

其它方法:......

2. 數(shù)組去重

描述:將數(shù)組中重復(fù)的元素過(guò)濾掉。

let array = [1, 2, 1, "3", "3", 0 , 1]
handle(array)   // [1, 2, "3", 0]

方法一

const handle = array => [...new Set(array)]
handle(array)   // [ 1, 2, "3", 0 ]

知識(shí)點(diǎn):Set

方法二

const handle = array => array.reduce((accumulator, currentValue) => {
    !accumulator.includes(currentValue) && accumulator.push(currentValue)
    return accumulator
}, [])
handle(array)   // [ 1, 2, "3", 0 ]

知識(shí)點(diǎn):Array.prototype.includes()

方法三

const handle = array => {
    let map = new Map()
    return array.filter(item => map.has(item) ? false : map.set(item))
}
handle(array)   // [ 1, 2, "3", 0 ]

知識(shí)點(diǎn)Map、Array.prototype.filter()

其它方法:......

3. 模擬bind實(shí)現(xiàn)
Function.prototype.bind = function () {
    let self = this, args = Array.from(arguments), context = args.shift();
    return function () {
        return self.apply(context, args.concat(...arguments))
    };
};

知識(shí)點(diǎn)apply、call、bind

4. 模擬New實(shí)現(xiàn)
const handle = function() {
    let fn = Array.prototype.shift.call(arguments)
    let obj = Object.create(fn.prototype)
    let o = fn.apply(obj, arguments)
    return typeof o === "object" ? o : obj;
}

知識(shí)點(diǎn)Object.create()

5. 格式化數(shù)字
const num = 123456789;
const handle = num => String(num).replace(/B(?=(d{3})+(?!d))/g, ",")
handle(num) // 123,456,789

知識(shí)點(diǎn)正則表達(dá)式String.prototype.replace()

6. 回文判斷
const num = 123456654321;
const str = "abababababab";
const handle = params => {
    let str_1 = String(params).replace(/[^0-9A-Za-z]/g, "").toLowerCase();
    let str_2 = str_1.split("").reverse().join();
    return str_1 === str_2 ? true : false
}
handle(num) // true
handle(str) // false

知識(shí)點(diǎn)String.prototype.split()、Array.prototype.join()

7. 函數(shù)節(jié)流 定時(shí)器
const handle = (fn, interval) => {
    let timeId = null;
    return function() {
        if (!timeId) {
            timeId = setTimeout(() => {
                fn.apply(this, arguments)
                timeId = null
            }, interval)
        }
    }
}

知識(shí)點(diǎn)window.setTimeout

時(shí)間戳
const handle = (fn, interval) => {
    let lastTime = 0
    return function () {
        let now = Date.now();
        if (now - lastTime > interval) {
            fn.apply(this, arguments)
            lastTime = now
        }
    }
}
8. 函數(shù)防抖
const handle = (fn, delay) => {
    let timeId;
    return function() {
        if (timeId) clearTimeout(timeId)
        timeId = setTimeout(() => {
            fn.apply(this, arguments)
        }, delay)
    }
}

函數(shù)節(jié)流、函數(shù)防抖區(qū)別:函數(shù)節(jié)流和函數(shù)防抖較容易混淆,可以這么比喻,對(duì)于函數(shù)節(jié)流,門(mén)外有人頻繁敲門(mén),但是門(mén)衛(wèi)按固定時(shí)間來(lái)決定是否開(kāi)門(mén)。對(duì)于函數(shù)防抖,門(mén)外有人頻繁敲門(mén),門(mén)衛(wèi)按最后一次敲門(mén)來(lái)決定是否開(kāi)門(mén)。

知識(shí)點(diǎn)window.clearTimeout

9. 發(fā)布訂閱模式
class Pubsub {
    constructor() {
        this.handles = {}
    }
    subscribe(type, handle) {
        if (!this.handles[type]) {
            this.handles[type] = []
        }
        this.handles[type].push(handle)
    }
    unsubscribe(type, handle) {
        let pos = this.handles[type].indexOf(handle)
        if (!handle) {
            this.handles.length = 0
        } else {
            ~pos && this.handles[type].splice(pos, 1)
        }
    }
    publish() {
        let type = Array.prototype.shift.call(arguments)
        this.handles[type].forEach(handle => {
            handle.apply(this, arguments)
        })
    }
}

const pub = new Pubsub()
pub.subscribe("a", function() {console.log("a", ...arguments)})
pub.publish("a", 1, 2, 3)
// a 1 2 3

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/102041.html

相關(guān)文章

  • 一篇字節(jié)跳動(dòng)前端面經(jīng)

    摘要:為了避免它,只需分配將要使用的必要構(gòu)造函數(shù)。示例對(duì)于此示例,就需要保持父構(gòu)造函數(shù)繼續(xù)正常工作。結(jié)論手動(dòng)設(shè)置或更新構(gòu)造函數(shù)可能會(huì)導(dǎo)致不同且有時(shí)令人困惑的后果。為了防止它,只需在每個(gè)特定情況下定義構(gòu)造函數(shù)的角色。 hr小姐姐說(shuō)一共有1輪筆試 + 3輪技術(shù)面 + 1輪hr面,面試地點(diǎn)在中關(guān)村天使大廈,崗位是1-3年前端 筆試 筆試分為多選 簡(jiǎn)答 判斷 手寫(xiě)代碼四部分,下面只寫(xiě)了印象比較深的幾...

    caige 評(píng)論0 收藏0
  • 記錄一下自己的春招,唯品會(huì)、360、京東offer已收、騰訊offer_call已達(dá)!??!

    摘要:春招結(jié)果五月份了,春招已經(jīng)接近尾聲,因?yàn)榈搅酥芪逋砩蟿偤糜锌?,所以?jiǎn)單地記錄一下自己的春招過(guò)程。我的春招從二月初一直持續(xù)到四月底,截止今天,已經(jīng)斬獲唯品會(huì)電商前端研發(fā)部大數(shù)據(jù)與威脅分析事業(yè)部京東精銳暑假實(shí)習(xí)生的騰訊的是早上打過(guò)來(lái)的。 春招結(jié)果 五月份了,春招已經(jīng)接近尾聲,因?yàn)榈搅酥芪逋砩蟿偤糜锌眨院?jiǎn)單地記錄一下自己的春招過(guò)程。我的春招從二月初一直持續(xù)到四月底,截止今天,已經(jīng)斬獲唯品...

    freewolf 評(píng)論0 收藏1
  • 我的春招求職經(jīng)驗(yàn)分享(已拿阿里京東網(wǎng)易等 5 個(gè) offer)

    摘要:面經(jīng)因?yàn)槲彝耆珱](méi)有面試經(jīng)驗(yàn),從來(lái)沒(méi)有經(jīng)歷過(guò)面試,于是想著在去這類(lèi)大公司面試之前先找成都的小公司練練手,積累點(diǎn)面試經(jīng)驗(yàn)。于是三月份開(kāi)始就有成都的小公司開(kāi)始約我面試。 前序 從我高考成績(jī)出來(lái)那一刻開(kāi)始,從我在高考志愿上填上計(jì)算機(jī)科學(xué)與技術(shù)這幾個(gè)當(dāng)時(shí)在心中堪稱(chēng)神圣的幾個(gè)字開(kāi)始,我就已經(jīng)把進(jìn)入中國(guó)互聯(lián)網(wǎng)最高殿堂BAT作為我整個(gè)大學(xué)奮斗的目標(biāo),哪怕我就讀的是一所位于內(nèi)陸的雙非一本大學(xué)我也認(rèn)為我能...

    Winer 評(píng)論0 收藏1
  • 前端面試手寫(xiě)代碼

    摘要:雖然構(gòu)造函數(shù)或者對(duì)象字面量的方法都可以用來(lái)創(chuàng)建對(duì)象,但是這些方法使用同一個(gè)接口創(chuàng)建很多對(duì)象,會(huì)產(chǎn)生大量的重復(fù)代碼。參考資料冴羽的專(zhuān)題系列中高級(jí)前端面試手寫(xiě)代碼無(wú)敵秘籍前端筆試之手寫(xiě)代碼一本系列會(huì)從面試的角度出發(fā)圍繞JavaScript,Node.js(npm包)以及框架三個(gè)方面來(lái)對(duì)常見(jiàn)的模擬實(shí)現(xiàn)進(jìn)行總結(jié),具體源代碼放在github項(xiàng)目上,長(zhǎng)期更新和維護(hù) showImg(https://use...

    niceforbear 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<