r/javascript • • Mar 09 '15

Things every JavaScript developer should know

http://ilikekillnerds.com/2015/03/things-every-javascript-developer-should-know/
326 Upvotes

118 comments sorted by

View all comments

-1

u/[deleted] Mar 09 '15

I disagree about call and apply though. I prefer to never use:

  • new

  • this (one exception)

  • Object.create

  • bind

  • call

  • apply

When I avoid this list my code tends to be smaller and faster to execute. You don't need OOP in this language, thanks to lexical scope, to achieve decomposable reuse and reference sharing.

The one exception for this is event handlers where it is commonly necessary to access the event target without access to an explicit reference.

4

u/oefig Mar 09 '15

Could you explain further in what ways bind/call/apply can affect performance? I love using bind to set up my handler functions like so:

$element.addEventListener('click',  someHandler.bind(this, 'foo'))

... over:

$element.addEventListener('click', function() {
    someHandler('foo');
});

I always thought .bind and .apply fit well into Javascript, but if I shouldn't be using them please tell me why!

2

u/cosinezero Mar 09 '15

Could you explain further in what ways bind/call/apply can affect performance?

Not quite sure I can explain the whys, but I can certainly show you the benchmarks with various ways of calling things: http://jsperf.com/javascript-bind-vs-function

Of interest to you is the poor performance of the very first test.

3

u/[deleted] Mar 09 '15

Thanks for this. All the more reason to avoid bind.

4

u/nschubach Mar 09 '15

Is it really? You have a click event on a button. If you are able to click that button a thousand times a second, I might see a reason to care about a few clock ticks of performance, but for the use case bind provides an easy readable way to implement event binding.