For..of iteration
for (let i of iterable) {
···
}
For iterating through generators and arrays.
See: For..of iteration
Generators
function* idMaker () {
let id = 0
while (true) { yield id++ }
}
let gen = idMaker()
gen.next().value // → 0
gen.next().value // → 1
gen.next().value // → 2
It's complicated.
See: Generators
Exports
export default function () { ··· }
// aka: module.exports.default = ···
export function mymethod () { ··· }
// aka: module.exports.mymethod = ···
export const pi = 3.14159
// aka: module.exports.pi = ···
export
is the new module.exports
.
See: Module exports
Generators
Imports
import 'helpers'
// aka: require('···')
import Express from 'cs/express'
// aka: const Express = require('···').default || require('···')
import { indent } from 'helpers'
// aka: const indent = require('···').indent
import * as Helpers from 'helpers'
// aka: const Helpers = require('···')
import { indentSpaces as indent } from 'helpers'
// aka: const indent = require('···').indentSpaces
import
is the new require()
.
See: Module imports
Extract values
const fatherJS = { age: 57, name: "Brendan Eich" }
Object.values(fatherJS)
// [57, "Brendan Eich"]
Object.entries(fatherJS)
// [["age", 57], ["name", "Brendan Eich"]]
Modules
Computed property names
let event = 'click'
let handlers = {
[`on${event}`]: true
}
// Same as: handlers = { 'onclick': true }
See: Object literal enhancements
Getters and setters
const App = {
get closed () {
return this.status === 'closed'
},
set closed (value) {
this.status = value ? 'closed' : 'open'
}
}
See: Object literal enhancements
Methods
const App = {
start () {
console.log('running')
}
}
// Same as: App = { start: function () {···} }
{: data-line="2"}
See: Object literal enhancements
Shorthand syntax
module.exports = { hello, bye }
// Same as: module.exports = { hello: hello, bye: bye }
See: Object literal enhancements
Fat arrows
Fat arrows
setTimeout(() => {
···
})
With arguments
readFile('text.txt', (err, data) => {
...
})
Implicit return
numbers.map(n => n * 2)
// No curly braces = implicit return
// Same as: numbers.map(function (n) { return n * 2 })
numbers.map(n => ({
result: n * 2
}))
// Implicitly returning objects requires parentheses around the object
Like functions but with this
preserved.
See: Fat arrows
Top comments (0)