Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Гармония кода с ECMAScript 6

Гармония кода с ECMAScript 6

More Decks by Андрей Листочкин (Andrey Listochkin)

Other Decks in Programming

Transcript

  1. ES6

  2. function UserService(baseUrl) { this.baseUrl = baseUrl; } UserService.prototype.getUser = function

    getUser(id, callback) { ... }; var userService = new UserService('/users');
  3. class UserService { constructor(baseUrl) { this.baseUrl = baseUrl; } getUser

    (id, callback) { ... } } let userService = new UserService('/users');
  4. define(function (require) { var _ = require('lodash'); var UserService =

    require('services').UserService; function UserController() { ... } return UserController; });
  5. import * as _ from 'lodash'; import UserService from 'services';

    class UserController { ... } export default UserController;
  6. const secret = Symbol(); export class Box extends Holder {

    get content () { return 'Some Content' } [secret]() { return `I'm a secret for ${this.content}`; } }
  7. const secret = Symbol(); export class Box extends Holder {

    get content () { return 'Some Content' } [secret]() { return `I'm a secret for ${this.content}`; } }
  8. const secret = Symbol(); export class Box extends Holder {

    get content () { return 'Some Content' } [secret]() { return `I'm a secret for ${this.content}`; } }
  9. const secret = Symbol(); export class Box extends Holder {

    get content () { return 'Some Content' } [secret]() { return `I'm a secret for ${this.content}`; } }
  10. const secret = Symbol(); export class Box extends Holder {

    get content () { return 'Some Content' } [secret]() { return `I'm a secret for ${this.content}`; } }
  11. const secret = Symbol(); export class Box extends Holder {

    get content () { return 'Some Content' } [secret]() { return `I'm a secret for ${this.content}`; } }
  12. const secret = Symbol(); export class Box extends Holder {

    get content () { return 'Some Content' } [secret]() { return `I'm a secret for ${this.content}`; } }
  13. let fibonacci = { [Symbol.iterator]: function* () { let pre

    = 0, cur = 1; for (;;) { let temp = pre; pre = cur; cur += temp; yield cur; } } } for (let n of fibonacci) { if (n > 1000) break; console.log(n); }
  14. let fibonacci = { [Symbol.iterator]: function* () { let pre

    = 0, cur = 1; for (;;) { let temp = pre; pre = cur; cur += temp; yield cur; } } } for (let n of fibonacci) { if (n > 1000) break; console.log(n); }
  15. let fibonacci = { [Symbol.iterator]: function* () { let pre

    = 0, cur = 1; for (;;) { let temp = pre; pre = cur; cur += temp; yield cur; } } } for (let n of fibonacci) { if (n > 1000) break; console.log(n); }
  16. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  17. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  18. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  19. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  20. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  21. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  22. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  23. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  24. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  25. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  26. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  27. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  28. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  29. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  30. co(function* () { const user = yield getUser(userId); const [

    posts, comments ] = yield [ getPosts(user.id), getComments(user.id) ]; return Object.assign(user, { posts, comments }); }).then((user) => { for (let [index, post] of user.posts.entries()) { this.render`${index} ⇒ ${post.title}`; } });
  31. let cpMap = posts.reduce((map, post) => map.set(post, new Set()), new

    Map()); comments.forEach(comment => cpMap.get(comment.post).add(comment)); cpMap.get(post) for (let [post, comments] of cpMap.entries()) { for (let comment of comments) { .. } }
  32. let cpMap = posts.reduce((map, post) => map.set(post, new Set()), new

    Map()); comments.forEach(comment => cpMap.get(comment.post).add(comment)); cpMap.get(post) for (let [post, comments] of cpMap.entries()) { for (let comment of comments) { .. } }
  33. let cpMap = posts.reduce((map, post) => map.set(post, new Set()), new

    Map()); comments.forEach(comment => cpMap.get(comment.post).add(comment)); cpMap.get(post) for (let [post, comments] of cpMap.entries()) { for (let comment of comments) { .. } }
  34. let cpMap = posts.reduce((map, post) => map.set(post, new Set()), new

    Map()); comments.forEach(comment => cpMap.get(comment.post).add(comment)); cpMap.get(post) for (let [post, comments] of cpMap.entries()) { for (let comment of comments) { .. } }
  35. let cpMap = posts.reduce((map, post) => map.set(post, new Set()), new

    Map()); comments.forEach(comment => cpMap.get(comment.post).add(comment)); cpMap.get(post) for (let [post, comments] of cpMap.entries()) { for (let comment of comments) { .. } }
  36. let cpMap = posts.reduce((map, post) => map.set(post, new Set()), new

    Map()); comments.forEach(comment => cpMap.get(comment.post).add(comment)); cpMap.get(post) for (let [post, comments] of cpMap.entries()) { for (let comment of comments) { .. } }