const
and let
over var
var
:const
and let
statements also declare variables. They were introduced in ES6.const
in cases where a variable is never re-assigned. Using const
makes this clear to whoever is reading your code. It refers to the "constant" state of the variable in the context it is defined within.let
.const
and let
instead of var
. In addition to the restriction introduced by const
, both const
and let
are block scoped as opposed to function scoped. This scoping can help avoid unexpected bugs.return
for the function.return
as it's implied.this
object.function () {}
) will bind this
in anonymous functions to the global object. To illustrate the confusion this causes, consider the following example:printSongs()
iterates over this.songs
with forEach()
. In this context, this
is bound to the object (jukebox
) as expected. However, the anonymous function passed to forEach()
binds its internal this
to the global object. As such, this.printSong(song)
calls the function declared at the top of the example, not the method on jukebox
.this
value of the enclosing context. Using an arrow function for printSongs()
has the expected result:import
/export
syntax.export
to specify a variable the module should expose. Here's an example of a file that exports two functions:import
. We need to specify which functions we want to import. A common way of doing this is using ES6's destructuring assignment syntax to list them out like this:saySomething
) is unavailable outside of the module.from
, indicating that the ES6 module is a local file as opposed to an npm package.export
before each variable you'd like to export, you can use this syntax to list off all the exposed variables in one area:import * as <Namespace>
syntax:react-dom
, you can import ReactDOM
(a default export) like this:render()
function, you can import the named render()
function like this:react-dom
looks something like this:code/webpack/es6-modules
.Object.assign()
Object.assign()
often throughout the book. We use it in areas where we want to create a modified version of an existing object.Object.assign()
accepts any number of objects as arguments. When the function receives two arguments, it copies the properties of the second object onto the first, like so:Object.assign()
. The first argument is a new JavaScript object, the one that Object.assign()
will ultimately return. The second is the object whose properties we'd like to build off of. The last is the changes we'd like to apply:Object.assign()
is a handy method for working with "immutable" JavaScript objects....
)...
operator will expand the array that follows into the parent array. The spread operator enables us to succinctly construct new arrays as a composite of existing arrays.undefined
when the function is called.b
in the example above is undefined
, the default argument is used. Note that null
will not use the default argument:'onion'
is ignored and has no variable bound to it.ingredients
and onClick
) that we then use inside the component's function body.