let [ a, b ] = [ 1, 2 ]
log( `${a} ${b}` ) 1 2
const [ , y, m, d ] = "2016-10-17".match( /(\d+)-(\d+)-(\d+)/ )
log( `y:${y}, m:${m}, d:${d}` ) y:2016, m:10, d:17
const data = { a: 1, b: 2, c: 3 }
let { a, b, c } = data;
log( `a:${a}, b:${b}, c:${c}` ) 1 2 3
const data = [ 1, 2, 3 ]
for (const x of data){
print(x)
} 1 2 3
let m = new Map([ ['a', 1], ['b', 2], ['c', 3] ])
log(m) Map { 'a' => 1, 'b' => 2, 'c' => 3 }
for (let [key, value] of m.entries()) {
print(`${key} => ${value}`)
} a => 1 b => 2 c => 3
const str = 'abc'
for(const x of str){
print(x)
} a b c
const chars = [...'abc']
log(chars) [ 'a', 'b', 'c' ]
const SYM1 = Symbol();
const SYM2 = Symbol( 'two' );
const SYM3 = Symbol( 'this is 3' );
const map = new Map
map[ SYM1 ] = 1
map[ SYM2 ] = 2
map[ SYM3 ] = 3
log( map[ SYM1 ] ) 1
log( map[ SYM2 ] ) 2
log( map[ SYM3 ] ) 3
let obj = {
red: Symbol('red'),
blue: Symbol('red'),
green: Symbol('red'),
}
log(obj.red == obj.red) true
log(obj.red == obj.blue) false
let [a,b] = [1,2]
log(a,b) 1 2
let { x, y } = { x: 1, y: 2 }
log( x, y ) 1 2
let { y } = { x: 1, y: 2 }
log( y ) 2
let nested = { a: { b: 10 } }
let { a: { b: x } } = nested
log( x ) 10
let [ x, ...y ] = 'abc'
log( x, y ) a [ 'b', 'c' ]
let { x = 1, y = 2 } = {}
log( x, y ) 1 2
let { x = 1, y = 2 } = { y: 200 }
log( x, y ) 1 200
let f = ( { a = 1, b = 1 } ) => a * b
log( f( {} ) ) 1
log( f( { a: 2 } ) ) 2
log( f( { a: 2, b: 2 } ) ) 4
let array = [ [ 0, 1 ], [ 2, 3 ] ]
for ( let [ x, y ] of array ) {
print( `[${x}, ${y}]` )
} [0, 1] [2, 3]
const items = [ [ 'foo', 3 ], [ 'bar', 9 ] ];
items.forEach( ( [ word, count ] ) => {
console.log( word + ' ' + count );
} );
const items = [
{ word: 'foo', count: 3 },
{ word: 'bar', count: 9 },
];
items.forEach( ( { word, count } ) => {
console.log( word + ' ' + count );
} );
for ( let { word, count } of items ) {
console.log( word + ' ' + count );
}
const obj = {
l(){ log('l') }
}
obj.l() l
let first = 'kevin'
let age = 15
let obj = { first, age }
log(obj) { first: 'kevin', age: 15 }
let fname = 'f'
let sym = Symbol()
let obj = {
[['m','y','v','a','r'].join('')]: 1,
[fname](){ log('in f') },
[sym](){ log('in symbol named function') }
}
obj.f() in f
log(obj.myvar) 1
obj[sym]() in symbol named function
const source = { f(){ log('in f') } }
const dest = {}
Object.assign(dest, source)
dest.f() in f
let myClass = class {}
log(new myClass()) myClass {}
class MyClass {
static f(){
log('in f')
}
}
MyClass.f() in f