您可以将原始函数传递给匿名函数,该函数返回可以访问原始函数的替换函数.
例如
parseInt = (function parseInt(original) {
return function (x) {
console.log("original would've returned " + original(x));
// just random 'new' functionality
return (x | 0) * 2;
};
}(parseInt));
Run Code Online (Sandbox Code Playgroud)
示例输出:
>> parseInt(10);
<< original would've returned 10
<< 20
Run Code Online (Sandbox Code Playgroud)