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.

12

u/Vheissu_ Mar 09 '15

It might not be a requirement to use call or apply, but I think as a developer it is important to know about them and how they work though. Until ES6 is better supported, we're stuck with call, apply and bind. Javascript is a complex beast, I think it is important to know about many facets even if you don't use them. Spread operators are the future.

1

u/[deleted] Mar 09 '15

Until ES6 is better supported, we're stuck with call, apply and bind.

I don't see why ES6 is a factor in that discussion. I have seen several posts comments in this sub-reddit that make it sound like developers need ES6 right now just to write code, as though they cannot possibly write JavaScript without something like Babel.js.

I am curious where this line of thinking comes from. Maybe those people are all incredibly new to JavaScript or maybe they have never learned to build large applications in this language without it looking like Java. I would like to learn more about why people are forming such conclusions.

2

u/moreteam Mar 09 '15

In the case of call/apply: They are the only way to "simulate" argument forwarding in ES5. For example the following ES6 code:

function traceAndCall(fn, ...args) {
  console.log("Called function");
  fn(...args);
}

To write something similar in ES5 you need apply. Or in other words:

Until ES6 is better supported, we're stuck with call, apply and bind.

0

u/[deleted] Mar 09 '15

Apply and call allow execution of a function by reference with something explicit passed in as a value for this. If you don't use this then you don't need call or apply. Call, apply, and bind are all new with ES5. I absolutely promise you can program large elegant decomposable applications without these.

2

u/moreteam Mar 09 '15

No, apply also allows passing varargs into a function. That it also supports passing in an explicit this context isn't important here. My example makes no reference to any this, so I'm confused why you would bring that up. Also, apply and call are not ES5. I'm not sure where that idea comes from. bind - yes. But the other two are ES3. But just out of curiosity - maybe I'm missing something. How would you implement the following ES3 snippet without apply or call?

function traceAndCall(fn) {
  console.log("Called function");
  fn.apply([].slice.call(arguments, 1));
}

I'm not saying that this is the kind of code you'd have all over the place. I'm just saying that you will be using it one way or another. Hiding it behind a lodash helper doesn't count.

P.S.: That snippet is the pre-ES6 version of the above ES6 code.

0

u/[deleted] Mar 09 '15 edited Mar 10 '15

It is generally considered bad form to pass arguments of unknown type or quantity into a function. This makes the reference invocation (function arguments are reference declarations) challenging to predict, which interferes with compilation in the JIT. I am sure this may be of great convenience to your code, but I would recommend against it.

2

u/MrBester Mar 09 '15

It is generally considered bad form to pass arguments of unknown type or quantity into a function.

FTFY