Weitere ähnliche Inhalte Ähnlich wie Serverside ES6@Livesense technight (20) Serverside ES6@Livesense technight14. • Block scoping
• Classes (strict mode only)
• Collections
• Typed arrays
• Generators
• Binary and Octal literals
• Object literal extensions
• Promises
• New String methods
• Symbols
• Template strings
• Arrow Functions
ES6 features ship with
Node.js by default(v4.2.1)
15. • Block scoping
• Classes (strict mode only)
• Collections
• Typed arrays
• Generators
• Binary and Octal literals
• Object literal extensions
• Promises
• New String methods
• Symbols
• Template strings
• Arrow Functions
ES6 features ship with
Node.js by default(v4.2.1)
17. • Block scoping
• 変数のスコープ、定数化
• letで宣言された変数は、その変数が定義されたブロックにスコープ
が限定される。
• constで定数化。
18. function varTest() {
var x = 31;
if (true) {
var x = 71; // same variable!
console.log(x); // 71
}
console.log(x); // 71
}
function letTest() {
let x = 31;
if (true) {
let x = 71; // different variable
console.log(x); // 71
}
console.log(x); // 31
}
19. const MY_FAV = 7;
// this will fail silently in Firefox and
Chrome (but does not fail in Safari)
MY_FAV = 20;
// will print 7
console.log("my favorite number is: " +
MY_FAV);
// trying to redeclare a constant throws an
error
const MY_FAV = 20;
// the name MY_FAV is reserved for constant
above, so this will also fail
var MY_FAV = 20;
21. function timeout(duration = 0) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration);
})
}
var p = timeout(1000).then(() => {
return timeout(2000);
}).then(() => {
throw new Error("hmm");
}).catch(err => {
return Promise.all([timeout(100),
timeout(200)]);
})
22. • Classes (strict mode only)
• 目玉商品。JavaScriptで(他の言語と同じような)クラスがかけ
るよ。
• 最高だよ。もうprototypeとか書かなくて済むよ。
• 書かなくて済むけど、prototypeの糖衣構文です。
23. class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a
noise.');
}
}
class Dog extends Animal {
speak() {
console.log(this.name + ' barks.');
}
}
24. • Arrow Functions
• 関数型っぽいfunctionの書き方、レキシカルなthis(thisの参照
先が、意図に反してグローバルオブジェクトになって、「あれ?こ
のインスタンスのプロパティ更新されないぞ?」「値が入ってない
ぞ?」というようなことにならない。そのメソッドが呼び出される
オブジェクトの中に閉じている状態)を簡単に扱える。
• というかthisが自動で関数にbindされる
25. function Person(){
this.age = 0;
setInterval(() => {
this.age++; // |this| properly refers to
the person object
}, 1000);
}
var p = new Person();
26. • Generators (Function)
• イテレーターを簡単に作れる特殊な関数。任意のタイミングで処理
を止められる。オブジェクト自身が今どこまで処理を進めたか覚え
ておき、next()メソッドを使って止めたところから先の処理を再度
実行する。
• 特に用事がなかったので僕は使いませんでした。
27. function* anotherGenerator(i) {
yield i + 1;
yield i + 2;
yield i + 3;
}
function* generator(i){
yield i;
yield* anotherGenerator(i);
yield i + 10;
}
var gen = generator(10);
console.log(gen.next().value); // 10
console.log(gen.next().value); // 11
console.log(gen.next().value); // 12
console.log(gen.next().value); // 13
33. "use strict";
const _ = require('underscore');
const express = require('express');
const router = express.Router();
const newsCollectionModel = require('../
models/newsCollection.model');
router.get('/', (req, res) => {
let newsCollection = new
newsCollectionModel();
newsCollection.get(req, (items) => {
res.json({
response: items
});
});
});
35. class News {
constructor(props) {
this.title = props.title;
this.url = props.url;
this.origin = props.origin;
this.image_uri = props.image_uri;
this.created_at = props.created_at;
}
...
}
36. _convertNewsModelToObject(items) {
return _.flatten(items).map((item) => item.toObject());
}
get(req, cb) {
let promises = _.map(urls[req.query.country],
(url) => { return fetch(url, (res) => { return
res.body.items }) });
Promise.all(promises)
.then((items) => _.flatten(items) )
.then((items) => this._filterFeedlyItemsByImageExistance(items) )
.then((items) => this._createNewsModelCollectionByFeedly(items) )
.then((items) => this._convertNewsModelToObject(items) )
.then((items) => this._sortByCreatedAt(items) )
.then((items) => cb(items), (err) => console.error(err.stack) );
}