Skip to content

Instantly share code, notes, and snippets.

@eimfach
Last active November 20, 2015 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eimfach/3538f50ed7b67d5816d2 to your computer and use it in GitHub Desktop.
Save eimfach/3538f50ed7b67d5816d2 to your computer and use it in GitHub Desktop.
ecma script 6 cheat cheet
/* for of loops and iterables */
for(let char/*can use destructuring here*/ of "hello"){
console.log(char); // unicode char
}
for(let value of [1,2,3]){
console.log(value);
}
/*for of is for iterable objects like arrays and collections like Map or Set,
it's the workhorse loop statement introduced with es6*/
// Sets are a collection of unique values
// Maps are a collections of key and values
// these objects all include all a new property method "object[Symbol.iterator]" and "object[next]"
let iterator = [1,2,3][Symbol.iterator]();
console.log(iterator.next());
/*every object can be used for the for of loop but
they need to have the iterator method */
for(let prop of Object.keys({name: 'rob'})){ // this returns an array which has the iterator method
console.log(prop);
}
// an custom iterable object would look e.g. like this
var zeroesForeverIterator = {
[Symbol.iterator]: function () {
return this;
},
next: function () {
return {done: false, value: 0};
}
};
//“Good artists copy, great artists steal.” —Pablo Picasso
/* Generators */
/* They are special functions (see the function* keyword)
which construct asynchron function iterators */
function* CustomerServiceMessenger(name){
let customerMessage = yield 'Hello '+name;
console.log(customerMessage);
customerMessage = yield 'Can you explain ?';
console.log(customerMessage);
customerMessage = yield 'Thank you and Goodbye!';
console.log(customerMessage);
return "[return value]" /* or some reference */;
}
let messenger = CustomerServiceMessenger('eimfach');
console.log("Press any key to call the CustomerServiceMessenger...");
window.addEventListener('keypress', function listener(e){
console.clear();
console.log('Calling...');
window.removeEventListener('keypress', listener);
window.addEventListener('keypress', function messageListener(e){
let message = messenger.next('[customer message]');
if(message.done !== true){
console.log(message.value)
//reply etc
} else {
console.log('Messaging done.', message.value);
window.removeEventListener('keypress', messageListener);
}
});
});
/* Make any object iterable with generators
(mind drop the default iterator interface implementation mentioned before)*/
var myIterable = {
models: [1,2,3],
[Symbol.iterator]: function*(){
yield this;
}
}
let iter = myIterable[Symbol.iterator]();
console.log(iter.next());
for(let {models} of myIterable){
console.log(models)
}
let NumberDrawer = {
[Symbol.iterator]: function*(){
while(true){
yield Math.floor(Math.random()*10);
}
}
}
let drawer = NumberDrawer[Symbol.iterator]();
let sequence = [...Array(100)].map(val => drawer.next().value).join('')
console.log(sequence)
// Array to Object conversion using lodash and an arrow function
_.reduce(tags, (obj, tag) => _.extend(obj, {[tag]: false}), {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment