Member-only story
Best of Modern JavaScript — Methods, IIFEs, and this
Since 2015, JavaScript has improved immensely.
It’s much more pleasant to use it now than ever.
In this article, we’ll look at the spread operator and functions in JavaScript.
Method Definitions Syntax for Methods
We should use the method definition syntax for methods.
For example, we can write:
const obj = {
foo() {},
bar() {}
}
to define methods in obj
.
This is the same as:
const obj = {
foo: function() {},
bar: function() {}
}
If we don’t need the value of this
, we can also write:
const obj = {
foo: () => {},
bar: () => {}
}
We used arrow functions so that we don’t have to worry about the value of this
in the function.
Avoid IIFEs in ES6
We don’t really need IIFEs in ES6 or later.
The most common use of IIFEs is to define private variables that are only available within a function.
In ES5, we have something like:
(function() {
var tmp = 'foo';
//...
}());