Skip to content

Instantly share code, notes, and snippets.

@egdelwonk
Last active May 12, 2017 22:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egdelwonk/226937f47122d06554c3 to your computer and use it in GitHub Desktop.
Save egdelwonk/226937f47122d06554c3 to your computer and use it in GitHub Desktop.
es2015 for python devs

Imports

Python:

import <module>
from <module> import defaultMember
from <module> import *
import <module> as <name>

ES2015:

import <defaultMember> from 'module';
import { <member> } from 'module';
import { <member> as <alias>} from 'module';
import * from 'module';
import * as <name> from 'module';

Generators

Python:

  def getNames():
      yield "Will"
      yield "Jason"
      yield "Brian"

ES2015:

  function *getNames() {
    yield "Will";
    yield "Jason";
    yield "Brian";
  }

Lambdas / Anonymous Functions

Python:

  # Syntax
  lambda x: x + 1

  # Usage
  plusOne = lambda x: x 1
  newAge = plusOne(30) # 31

ES2015

  # ES5 Syntax
  function(x) {
    return x + 1
  }
  
  # Usage
  let plusOne = function(x) {
    return x + 1;
  };
  plusOne(30) // 31
  

  # ES2015 Syntax ( Arrow Function)
  x => x + 1

  # Usage
  let plusOne = x => x + 1;
  plusOne(30) // 31

  *Note* One thing to know, is when you use an arrow function,
  *this* is bound to the outer calling scope.

Closures

Python:

  def addX(x):
    return lambda y: x + y

  plusOne = addX(1);
  plusOne(1) # 2

ES2015

  # ES5
  var addX = function (x) {
    return function (y) {
        return x + y;
    };
  };
  var plusOne = addX(1);
  plusOne(1) // 2

  # ES2015 (with Arrow Functions)
  let addX = x => y => x + y;
  var plusOne = addX(1);
  plusOne(1) // 2

Destructuring

Python:

  # Tuple Destructuring
  name, age = ('will', 31)
  # name -> 'will', age -> 31
  
  # List Destructuring
  name, age = ['will', 31]
  # name -> 'will', age -> 31

  # Object Destructuring
  name, age = {'name': 'will', 'age': 31}
  # name -> 'will', age -> 31

ES2015:

  // List Destructuring
  let [name, age] = ['will', 31]
  // name -> 'will', age -> 31

  // Object Destructuring
  let {name, age} = {'name': 'will', 'age': 31}
  // name -> 'will', age -> 31


  // List Destructuring with spread
  let [name, age, ...rest] = ['will', 31, 'apples', 'oranges']
  // name -> 'will', age -> 31
  // rest -> ["apples", "oranges"]

Spread Operator

Python:

  def say_hello(name, age, color):
    print "Hello, {0}! You are {1}. Your fav. color is {2}".format(name, age, color)

    params = ['will', 31, 'green']
    say_hello(*params) # 'Hello, will! You are 31. Your fav. color is green'

ES2015:

  function sayHello(name, age, color){
    console.log(`Hello, ${name}! You are ${age}. Your fav. color is ${color}`);
  }

  let params = ['will', 31, 'green']
  sayHello(...params) // 'Hello, will! You are 31. Your fav. color is green'

  *Note* The spread operator is really powerful. Check out these other examples:

  // Array Literals
  let friends = ['will', 'jason', 'brian'];
  let members = ['bill', ...friends, 'matt'];
  // members -> ['bill', 'will', 'jason', 'brian', 'matt']

 // Push Arrays

  let myFriends = ['brian', 'jason'];
  let ourFriends = ['matt', 'bill'];
  myFriends.push(...ourFriends)
  // myFriends -> ['brian', 'jason', 'matt', 'bill']

Classes

Python:

  class Friend(Person):
      def __init__(self, name):
        super(Person, self).__init__(name)
        self.name = name
      def say_hello(self):
        print "Your friend {0} said hello!".format(self.name)

ES2015:

  class Friend extends Person {
    constructor(name) {
      super(name);
      this.name = name;
    }

    sayHello() {
      console.log(`Your friend ${this.name} said hello!`);
    }
  }

More ES2015

  • Lexical This

    • This is bound to the outer calling scope.
  • Enhanced Object Literals:

    • Set Prototype at Construction
    • Shorthand for property assignments
    • Shorthand for method definitions
    • Super Calls
    • Computed Dynamic Properties
  • Template Strings

    • String Interpolation
    • Multiline Support
  • Object Destructuring:

    • Defaults
  • Default paramaters

function wave(name, greeting="hello") {
  console.log(`${greeting}! ${name}, how are you?`);
}
  • Block Scoped Binding

    • Let scoping
    • Const is single assignment
  • Iterators

  • Modules

    • Expand on import system
    • Explain export / export as default
  • New Data Structures

    • Map
    • Set
    • WeakMap
    • Weakset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment