goodeggs / bites

Technical morsels
http://bites.goodeggs.com
8 stars 4 forks source link

Monkey patching event listeners #19

Open asalant opened 10 years ago

asalant commented 10 years ago

I first came up with this technique when working on my reimplementation of the coffee-script REPL to use the node REPL instead of the hacked together REPL that coffee-script came with.

https://github.com/jashkenas/coffee-script/blob/master/src/repl.coffee#L50-L59

It was required to provide multiline support for the coffee REPL. You can essentially replace an existing event listener with your own that then delegates to the original event listener. Handy when you need to patch existing behavior.