Syntactic sugar makes familiar operations shorter or clearer to write without adding underlying capabilities. Compare the surface syntax with the operation it abbreviates to understand what the runtime is doing. section 7.4.1 section 6.2.1
JavaScript uses syntactic sugar for several different tasks:
- ES6 classes provide a more elegant way to write prototype-based objects; equivalent ES5 code uses constructor functions and prototype methods. section 7.4.1
- Arrow functions shorten function expressions, as in a sorting callback written with `(value1, value2) => value1 - value2`. section 3.3.2
- A for-of loop handles iterator results behind the scenes instead of repeatedly calling `next()` and checking `done`. section 6.2.1
Listing 7.13. Creating a class in ES6
class Ninja{ #A
constructor(name){ #B
this.name = name; #B
} #B
swingSword(){ #C
return true; #C
} #C
}
var ninja = new Ninja("Yoshi"); #D
assert(ninja instanceof Ninja, "Our ninja is a Ninja"); #E
assert(ninja.name === "Yoshi", "named Yoshi"); #E
assert(ninja.swingSword(), "and he can swing a sword"); #E
See a Ninja class, instance creation, and checks of its properties and method. section 7.4.1
var values = [0, 3, 2, 5, 7, 4, 8, 1];
values.sort((value1,value2) => value1 – value2);
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":46}]]"}
!@%STYLE%@!
Compare a sorting callback written as a function expression and as an arrow function. section 3.3.2
for(var item of WeaponGenerator ()){
assert(item !== null, item);
}
See a for-of loop consume values from a generator. section 6.2.1

