- Become familiar with a subset of ES6
- Learn where to find more about ES6
- Introduce object spread, a popular feature not yet part of the standard
We'll be using the ES6 Katas for some of the exercises. If an environment isn't specified, use the Chrome web console.
To play along, refer to the MDN docs
- Fix the broken tests in the ES6 Katas section on
let
. - Fix the broken tests in the section on const
Convert the following to ES6 code using template strings:
// feel free to fill in your own details :-)
const town = "Burlington";
const name = "Matt";
const age = 30;
console.log("I live in", town);
console.log("My name is " + name + ". I am " + age + " years old");
- Create an arrow function that takes
name
andage
and print (console.log
) the sentence from exercise 2. - Create an arrow function that takes the following object and returns an object with all of the keys capitalized.
{ name: 'Matt', age: 30, town: 'Burlington', }
- Use an arrow function to multiply every number in the following array by 10:
[1, 2, 3, 4, 5]
- Use ES5 methods to rewrite the below constructor. Every time we enter
p.age
we should see a new age (age should increment by 1 every second), but with the way the constructor has been written,p.age
will always evaluate to 0. HINT: what doesthis
evaluate to outside of the contructor?function Person() { this.age = 0; setInterval(function growUp() { this.age++; }, 1000); } var p = new Person();
- Use an arrow function to fix the constructor.
- Complete the default parameters kata
In a single statement, do the following:
- Assign
city
andstate
to the first and second item in the following array['Burlington', 'Vermont']
- Alias
city
astown
in the above example - Provide a default value for state so that the following array also gets assigned a state:
['Richmond']
- Assign
city
andstate
to the values of the keys of the same name in the following object:{ city: 'Burlington', state: 'Vermont', country: 'United States', planet: 'Earth', }
- Alias
city
astown
in the above example - Assign
name
andfirstChildsName
to the respective values from the following object:{ name: 'Bob', age: 45, children: [ { name: 'Sue', age: 12, }, { name: 'Bobby Jr.', age: 8, } ] }
- Modify the above example to assign
No Children
tofirstChildsName
when passed an object with no children:{ name: 'Matt', age: 30, children: [], }
Fix the tests in the array spread kata
- Use Object.assign to overwrite the below object with your own info:
const person = {
firstName: 'Matt',
lastName: 'Parrilla',
city: 'Burlington',
state: 'Vermont',
}
- Use Object.assign to create a new object that overrides the object's values but leaves the original object intact.
- Use object spread to create the new object (you can use the Babel REPL to use object spread since it hasn't landed in Chrome yet)
Use babel to transpile your use of object spread into ES5.
ES6 was a huge change and we didn't cover all of it. The great news is, there are a lot of wonderful resources out there. Check out Ben Glassman's BTVJS talk for a great starting point and overview of ES6 features including some not covered above.
As usual, the MDN docs are great and should be a go-to resource for both ES6 specifically and javascript generally.