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
classes
you 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
iterator
as 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 in
orfor each
also for people that isn't too much into functional methods like map, reduce, ans such,
for of
is quite a savior.on the protocol vs interface, I think protocol is better suited in
jsland
where 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
const
overlet
orvar
? What are the actual advantages of it (other than telling the developer that it will not change)?Hey Patrick!
Thanks for the great question. I will try to answer by breaking it down first with
var
vslet
andconst
var
is function scoped.let
andconst
are block scoped. This is the biggest difference, to use the wise words of a work colleague: "let
is whatvar
should have been from the start."That said the main difference with
let
vsconst
is thatconst
does 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
let
and never change or re-declare it, the error will be to use aconst
.I hope I answered your question sufficiently.
Cheers!
const
has a great advantage: there are less moving parts in your code.I now default to
const
unless I know the variable is going to be reassigned later on.Patrick,
I was re-reading my post and noticed I am inconsistent with my
let
andconst
usage. I opened an issue and I am welcome to PR'sAgain thank you for reading and asking a question.
The scoping component of
let
seems pretty beneficial.let
andconst
seem 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
failed
function within athen
or the use of acatch
to 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
.then
and.catch
happen 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
.catch
blocks.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
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!
What is nice with
js
is that every day you learn a new thing. I never usedconst
before, nice. Thanks for those tips. :)Great article, feel free to check my article on ES5: codespot.org/javascript-101-es6-an...