Professional Documents
Culture Documents
JavaScript
Slightly more like Python
My name is Trey. I'm going to show you how JavaScript is becoming more like Python. A word of
warning: I will be going very quickly and there are code examples on almost every slide.
ECMAScript History
Edition Release Date Browser Support a.k.a.
3 Dec. 1999 All ES3
5 Dec. 2009 IE 9, FF 4, Chrome 7 ES5
6 June 2015 Not supported yet ES2015
ECMAScript is the language specification that JavaScript is based on. In this presentation I'm going to
show off features that were added in ECMAScript 6, also known as ECMAScript 2015. ECMAScript 6
isn't yet supported by browsers, but you **can** start using it today...
Fat Arrows
Python
JS
Our first new feature is fat arrows. Fat arrows are a new shortened function syntax. So fat arrows can be
used kind of like lambda functions. Fat arrows also have a longer syntax, but I'm only showing off the
short one in my slides. We don't use lambdas very often in Python partially because we don't use callbacks
often. In JavaScript we use callbacks *all the time*, so fat arrows are really useful.
Fat Arrows
Python
JS
When you combine fat arrow functions with JavaScript's array methods, they can serve the same purpose
as list comprehensions. In addition to being easier on the eyes, fat arrow functions inherit the "this"
binding of their outer scope. For anyone who has wrestled with JavaScript function binding, that's a really
big deal.
Template Strings
Python
JS
With Template strings you can now do string interpolation in JavaScript. You create template strings using
backticks. Unlike Python's format method, template strings interpolate using the current scope
automatically.
Template Strings
Python
JS
Template strings also allow you to make multi-line strings. So you can use template strings like triple-
quoted strings in Python.
Classes
Python
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return "({x}, {y})".format(x=self.x, y=self.y)
JS
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
JavaScript did not used to have a class syntax. It does now. This is what it looks like. Of course no class
implementation is complete without a properly-loaded foot gun. So JavaScript also supports...
Subclasses
Python
class ColorPoint(Point):
def __init__(self, x, y, color):
super().__init__(x, y)
self.color = color
def __str__(self):
return "{super} in color {color}".format(
super=super().__str__(),
color=self.color
)
JS
Inheritance. I actually think the "super" syntax is a little more elegant in JavaScript than it is in Python.
Spread Operator
Python
import datetime
date_fields = (2015, 6, 12);
date = datetime.date(*date_fields)
JS
JavaScript now has a spread operator. This is similar to Python's argument unpacking operator. In Python
this is an asterisk, in JavaScript this operator is three dots. You can use it to unpack an array into the
arguments of a function call.
Combine Arrays
Python
first = [1, 2]
second = [3, 4]
combined = first + second
JS
You can also use the spread operator to unpack one array inside of another array. In this example
`combined` would be the array `[1, 2, 3, 4]`.
Rest Operator
Python
def most_difference(*numbers):
return max(numbers) - min(numbers)
JS
function mostDifference(...numbers) {
return Math.max(...numbers) - Math.min(...numbers);
}
The rest operator is basically the opposite of the spread operator. Again instead of an asterisk, this
operator is three dots. This allows us to make functions that take a variable number of arguments. For
example our `mostDifference` function here takes any number of arguments. This code example shows
both the "rest" operator used once and "spread" operator used twice.
Default Arguments
Python
def greet(name="world"):
return "Hello {name}!".format(name=name)
JS
function greet(name='world') {
return `Hello ${name}!`;
}
JavaScript now allows you to specify default parameter values. This **does not** mean JavaScript has
keyword arguments. It doesn't. These are just default values for positional arguments.
Iterable Destructuring
Python
coordinate = (1, 4, 2)
x, y, z = coordinate
JS
Just like Python, JavaScript now has iterable unpacking. You can unpack arrays into multiple variables
and you can also use this for multiple assignment and variable swapping. Here, x, y, and z would be 1, 4,
and 2.
Decorators
Python
@memoize
def get_divisors(number):
return big_computation(number)
JS
@memoize
function getDivisors(number) {
return bigComputation(number);
}
This one is actually wishful thinking because this is only a proposed feature. The currently proposed
syntax for JavaScript decorators looks just like Python's decorator syntax. So *hopefully* JavaScript will
have decorators soon.
Modules
Python
import urls
from utils import jsonify, memoize
JS
JavaScript **actually** has modules now. They're properly namespaced and they work pretty similarly to
the way modules work in Python.
So I just showed you a small handful of new features supported by JavaScript. You can learn more about
Babel and try it out right from your web browser by going to the Babel website.
Final
http://treyhunner.com/js-like-python