jquery - How to add a callback function to an existing JavaScript function without adding code to the function? - Stack Overflow

Let say I have foo(). Is there a way to execute bar() after foo() without modifying foo()?foo(){ * lo

Let say I have foo(). Is there a way to execute bar() after foo() without modifying foo()?

foo(){ /* logic */}

bar(){ /* more logic *}

afterEvent1AlwaysExecute2(foo,bar);

Any non-jQuery or jQuery ments are appreciated :)

Let say I have foo(). Is there a way to execute bar() after foo() without modifying foo()?

foo(){ /* logic */}

bar(){ /* more logic *}

afterEvent1AlwaysExecute2(foo,bar);

Any non-jQuery or jQuery ments are appreciated :)

Share Improve this question edited Nov 17, 2022 at 23:10 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 19, 2012 at 1:09 jdpubjdpub 312 bronze badges 1
  • You could always wrap foo inside a new function which will call the old foo, and then call bar. Then you change the name of this new function to foo. As far as the outside world is concerned foo should work as expected, but secretly your replaced implementation will also call bar. Win! – Anurag Commented Jul 19, 2012 at 1:14
Add a ment  | 

1 Answer 1

Reset to default 9

You can do it without adding code to foo, by reassigning the name foo:

var original_foo = foo;
var foo = function() { original_foo(); bar(); }

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信