js arguments All in One
function rewriteHistory (type) {
const origin = window.history[type];
return function () {
const rs = origin.apply(this, arguments);
// ✅ 1. 添加自定义事件
const e = new Event(type.toLocaleLowerCase());
e.arguments = arguments;
// ✅ 2. 自动触发自定义事件
window.dispatchEvent(e);
return rs;
};
}
arguments
const log = console.log;
function func(a, b, c) {
log(`this =`, this);
// "this =" [object Window]
log(`arguments =`, arguments);
// "arguments =" Object { 0: 1, 1: 2, 2: 3 }
log(arguments[0]);
// 1
log(arguments[1]);
// 2
log(arguments[2]);
// 3
}
CustomEvent
setCustomEvent (type = 'click', data = {}) {
const customEvent = new CustomEvent(type, {
detail: data,
});
window.dispatchEvent(customEvent);
}