美文网首页前端面试基础必备
三七互娱的一道笔试题

三七互娱的一道笔试题

作者: puxiaotaoc | 来源:发表于2018-09-11 14:03 被阅读15次
    var Hero = function(name) {
        if (this.constructor.name !== 'Hero') {
          return new Hero(name)
        }
        this.name = name;
        (function() {
          console.log(`Hi! this is ${name}`)
        })()
        return Promise.resolve(this)
      }
    
      Promise.prototype.kill = function(num) {
        return this.then(() => {
          if (num & 1 !== 0) {
            console.log(`kill ${num} 注意这里是单数`)
            return this
          }
          console.log(`kill ${num} 注意这里是复数`)
          return this
        })
      }
    
      Promise.prototype.recover = function(num) {
        return this.then(() => {
          console.log(`Recover ${num} bloods`)
          return this
        })
      }
    
      Promise.prototype.sleep = function(num) {
        return this.then(() => new Promise(resolve => {
          var timer = setTimeout(() => {
            clearTimeout(timer)
            console.log(`sleep ${num} 秒`)
            resolve(this)
          }, num * 1000)
        }))
      }
    
    运行结果
    参考:https://www.nowcoder.com/discuss/104690

    相关文章

      网友评论

        本文标题:三七互娱的一道笔试题

        本文链接:https://www.haomeiwen.com/subject/eihugftx.html