3 ms·
Why are anonymous functions so popular in JS? Why not just name the callback?
by nerdponx 9y ago
Why are anonymous functions so popular in JS? Why not just name the callback?
- quotemstr 9y agoBecause Java is a kingdom of nouns, and for the most part (method handles, which are just fancy reflection objects, aside), you can't name a function without creating a closure. Anonymous callback: Foo.foo(() -> bar()); Named callback: private final class MyFooDelegate implements FooDelegate { @Override public void doFoo() { bar(); } } ... Foo.foo(new MyFooDelegate())
- pjmlp 9y agoYou forgotten about method handles.
- deleted 9y ago[deleted]
- dingo_bat 9y agoWhy name it if the name will never be used?
- wereHamster 9y agoThe name shows up in the stack trace, and you'll have a much better idea which path the code hit when it crashed.
- z3t4 9y agonaming things are hard and some programmers are willing to sacrifice their left hand if it means they can write shorter syntax.