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.
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.
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.
Fat arrow functions lexically bind this value, so it is more predictable for people coming from other languages.
When you have window as a this value it doesn't help anyone, it's clearly not intended, and depending in the caller of the function to call it correctly is risky
I completely agree, which is more reason to avoid to my above mentioned list.
I can understand why that would make ES6 more desirable, but not why so many developers on here sound as if they cannot live without it. How can people not live without it if these features are less than a year old in the browser and ES6 is not even finished yet?
It's hard to explain, but for me is all about familiarity, my first language was Java, and I also use Java on the server. So when I write this I expect it to be something. Using ES6 short function syntax I can write safer code. I could write Javascript without using this but is not a real alternative when you use most of the real world frameworks.
And the short syntax helps to improve readability and reusability of functions!
See, that's a problem. It's all very nice having these short forms, but readability for those used to a different language is not a valid reason for having them. Does Spanish add arbitrary constructs to make it easier for English speakers?
-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.