一道字节面试题解 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a Javascript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
Javascript 权威指南第 5 版
Closure: The Definitive Guide
xiaoming1992
V2EX    Javascript

一道字节面试题解

  •  1
     
  •   xiaoming1992 2020-06-01 21:35:24 +08:00 2210 次点击
    这是一个创建于 2033 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚刚面了字节跳动, 题目都很(~~~~~~)基础, 但就是答得不好...视频一挂, 诶, 有好想法了 T.T

    不行, 我非得把答案贴这儿, 可惜面试官不在我面前, 不然我非得告诉他, 你看, 这解答至少还凑合啊

    // 题目是实现下列链式调用 const chain = new Chain() chain .eat() .sleep(1000) .eat() .sleep(1000) .work() 
    // 我原来用的 promise 把所有方法都包裹起来, 简直丑破天际... // 下面是新的解答 // 至于里面有些属性是不是要用 private 或者 protected 什么的, 我就懒得细究了 interface EatAction { name: "eat"; params: []; } interface SleepAction { name: "sleep"; params: [number]; } interface WorkAction { name: "work"; params: []; } type Action = EatAction | SleepAction | WorkAction class Chain { running = false actions: Action[] = [] trigger() { if (this.running) { return } const action = this.actions.shift() if (!action) { return } this.running = true switch (action.name) { case "eat": console.log("eat") this.running = false this.trigger() break case "work": console.log("work") this.running = false this.trigger() break case "sleep": setTimeout(() => { console.log("sleep") this.running = false this.trigger() }, action.params[0]) break default: break } } eat() { this.actions.push({ name: "eat", params: [], }) this.trigger() return this } sleep(n: number) { this.actions.push({ name: "sleep", params: [n], }) this.trigger() return this } work() { this.actions.push({ name: "work", params: [], }) this.trigger() return this } } 
    jason94
        1
    jason94  
       2020-06-01 22:15:25 +08:00
    这样是不是简单一点

    ```ts
    class Chain {
    private actions: Function[] = [];
    constructor() {
    setTimeout(async () => {
    for (const action of this.actions) {
    await action.call(this);
    }
    });
    }

    eat() {
    this.actions.push(() => console.log('eat'));

    return this;
    }

    sleep(timeout: number) {
    this.actions.push(() => {
    console.log('sleep ', timeout);
    return new Promise((resolve) =>
    setTimeout(() => {
    resolve();
    }, timeout)
    );
    });
    return this;
    }
    work() {
    this.actions.push(() => console.log('work'));

    return this;
    }
    }

    const chain = new Chain();

    chain.eat().sleep(1000).eat().sleep(1000).work();

    ```
    xiaoming1992
        2
    xiaoming1992  
    OP
       2020-06-01 22:33:16 +08:00
    @jason94 你这么写倒也挺好, 就是如果有在 constructor 之后才添加进去的事件, 就不会执行了, 如:
    setTimeout(() => { chain.eat() }, 5000)
    mxT52CRuqR6o5
        3
    mxT52CRuqR6o5  
       2020-06-01 23:04:09 +08:00
    ```ts
    class Chain{
    constructor(promise){
    if(promise){
    this._promise=promise;
    }else{
    this._promise=Promise.resolve();
    }
    }
    eat(){
    return this._next(()=>{console.log('eat')});
    }
    work(){
    return this._next(()=>{console.log('work')});
    }
    sleep(ms){
    return this._next(async ()=>{
    await new Promise(resolve=>setTimeout(resolve,ms))
    });
    }
    _next(someAction){
    const nextPromise=(async ()=>{
    await this._promise;
    await someAction();
    })()
    return new Chain(nextPromise);
    }
    }
    ```
    我的思路,没测试,可能有 bug
    treemonster
        4
    treemonster  
       2020-06-01 23:06:56 +08:00   2
    class Chain{
    task=Promise.resolve()
    eat() {
    this.task=this.task.then(_=>console.log('eat'))
    return this
    }
    work() {
    this.task=this.task.then(_=>console.log('work'))
    return this
    }
    sleep(t) {
    this.task=this.task.then(_=>new Promise(r=>{
    console.log('sleep', t)
    setTimeout(r, t)
    }))
    return this
    }
    }

    promise 写很简单
    xiaoming1992
        5
    xiaoming1992  
    OP
       2020-06-02 00:00:33 +08:00
    @mxT52CRuqR6o5 @treemonster 大概一瞄,应该是没什么问题的。我当时的思路就跟 4 楼的一样,可是写的丑多了。。。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     957 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 19:09 PVG 03:09 LAX 11:09 JFK 14:09
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86