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';
Python:
def getNames():
yield "Will"
yield "Jason"
yield "Brian"
ES2015:
function *getNames() {
yield "Will";
yield "Jason";
yield "Brian";
}
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.
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
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"]
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']
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!`);
}
}
-
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