Introduction
This is not meant to replace the official documentation.
This post does not cover all the ES6 features.
For typos and cor...
For further actions, you may consider blocking this person and/or reporting abuse
Great notes!
If I may add: regarding to
classesyou might want to try something like this if you want to accept an options like constructorof course if you are using typescript you can extend that to use an interface and ensure you only accept what is required.
What are your thoughts on
iteratoras a protocol and not an interface?it is pretty useful, I think it has become better to work with collections, if you have teamates with python background, they can easily recognize
[...someIterable]also it lets you do some stuff likeconst keys = [...Object.keys(myObj)]withoutfor inorfor eachalso for people that isn't too much into functional methods like map, reduce, ans such,
for ofis quite a savior.on the protocol vs interface, I think protocol is better suited in
jslandwhere interfaces only exist on agreements (which some times not everyone agrees the same thing), not enforced in code.Great write-up! What are your opinions on using
constoverletorvar? What are the actual advantages of it (other than telling the developer that it will not change)?consthas a great advantage: there are less moving parts in your code.I now default to
constunless I know the variable is going to be reassigned later on.Hey Patrick!
Thanks for the great question. I will try to answer by breaking it down first with
varvsletandconstvaris function scoped.letandconstare block scoped. This is the biggest difference, to use the wise words of a work colleague: "letis whatvarshould have been from the start."That said the main difference with
letvsconstis thatconstdoes not allow re-declaring. This does tell a developer that it will not change as you mentioned, but more importantly, it is telling your program that it cannot change.Some linters out there (AirBnB) will throw an error if you declare a
letand never change or re-declare it, the error will be to use aconst.I hope I answered your question sufficiently.
Cheers!
Patrick,
I was re-reading my post and noticed I am inconsistent with my
letandconstusage. I opened an issue and I am welcome to PR'sAgain thank you for reading and asking a question.
The scoping component of
letseems pretty beneficial.letandconstseem to remove some of the wishy-washiness of JS.I totally agree. Block scoping is great progress
As someone who tries to write as little JavaScript as possible, I'm not sure I'm looking forward to infinite chains of impossible-to-debug promises any more than I currently enjoy infinite chains of impossible-to-debug callbacks.
Hey Ian,
Thanks for taking time to leave a comment, what part of a Promise Chain are you finding impossible to debug? Promises offer a second
failedfunction within athenor the use of acatchto manage errors. Maybe I can help clear up some confusion.In my opinion a promise chain is relief from callback hell.
I've not actually tried using them yet, but my comment mostly comes from looking at that chain, imagining a longer one, and then imagining trying to work out which
.thenand.catchhappen at which level, much like trying to work out which level of}) } } }) ) }, }; } } })the problem is in with callback hell.I guess it should at least be easier to add good error reporting in the
.catchblocks.I recommend trying promises out, start by working with promises before creating them. A promise that you could work with is the fetch api for example, google has a good introduction
What is nice with
jsis that every day you learn a new thing. I never usedconstbefore, nice. Thanks for those tips. :)Great post, can you change code block representation of example code to runkit.
Hey Kambala!
I am unfamiliar with runkit. Feel free to fork the repo and submit a PR though as an example. I might be able to do this with your help. The project is open source
pull request.Please check out.
On a work trip. I'll check it out this weekend. Thanks for your work!
Great article, feel free to check my article on ES5: codespot.org/javascript-101-es6-an...