js 钩子(hook)


var setHook = (function () {
function doSomeThing(attr, doSomeThings) {
var tempFunction = this[attr];
var that = this;
this[attr] = function () {
doSomeThings.apply(that, Array.prototype.slice.call(arguments));
return tempFunction.apply(that, Array.prototype.slice.call(arguments));
}
}

function setHook(obj, attr, doSomeThings) {
doSomeThing.call(obj, attr, doSomeThings);
}

return setHook;
})()


setHook(Math, 'max', function () {
console.log(arguments)
})

alert(Math.max(2, 3, 4, 5, 6, 7, 8))//8

ok!自己写的 有什么错误希望指正哈!

原文地址:https://www.cnblogs.com/dingzhipeng/p/8480393.html