Alias method chain in JavaScript? - Stack Overflow

In JavaScript, how could you create a new function with the same name as an existing function, while al

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 0
Add a ment  | 

3 Answers 3

Reset to default 8

You 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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744792595a4594007.html

相关推荐

  • Alias method chain in JavaScript? - Stack Overflow

    In JavaScript, how could you create a new function with the same name as an existing function, while al

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信