admin管理员组文章数量:1410717
In JavaScript, how could you create a new function with the same name as an existing function, while also preserving the original function so it could be called from within the new one?
In JavaScript, how could you create a new function with the same name as an existing function, while also preserving the original function so it could be called from within the new one?
Share Improve this question asked Jun 10, 2010 at 2:13 user65663user65663 03 Answers
Reset to default 8You can pass the original function into an anonymous function which returns a replacement function which has access to the original function.
E.g.
parseInt = (function parseInt(original) {
return function (x) {
console.log("original would've returned " + original(x));
// just random 'new' functionality
return (x | 0) * 2;
};
}(parseInt));
Example output:
>> parseInt(10);
<< original would've returned 10
<< 20
You want to implement function wrapping, check the following articles:
- Wrapping Functions in JavaScript
- Function Wrapping
You could simply assign the old function to a variable with a different name:
var old_parseInt = parseInt;
function parseInt(s) {
return old_parseInt(s) + 1;
}
本文标签: Alias method chain in JavaScriptStack Overflow
版权声明:本文标题:Alias method chain in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744792594a2625380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论