Python syntax here : 2.7 - online REPL

Javascript ES6 via Babel transpilation - online REPL

Imports

import math
print math.log(42)

from math import log
print log(42)

# not a good practice (pollutes local scope) :
from math import *
print log(42)
import math from 'math';
console.log(math.log(42));

import { log } from 'math';
console.log(log(42));

import * from 'math';
console.log(log(42));

Range

print range(5)
# 0, 1, 2, 3, 4
console.log(Array.from(new Array(5), (x,i) => i));
// 0, 1, 2, 3, 4

Generators

def foo():
    yield 1
    yield 2
    yield 3
function *foo() {
    yield 1;
    yield 2;
    yield 3;
}

Lambdas

lambda a: a * 2
a => a * 2

Destructuring

status, data = getResult()
var [status, data] = getResult();

Spread

search_db(**parameters)
searchDb(...parameters);

Iterators

def fibonacci():
    pre, cur = 0, 1
    while True:
        pre, cur = cur, pre + cur
        yield cur

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

Classes

(Python has builtin support for multiple inheritance)

class SpiderMan(Human, SuperHero):
    def __init__(self, age):
        super(SpiderMan, self).__init__(age)
        self.age = age
    def attack(self):
        print 'launch web'
class SpiderMan extends SuperHero {
    constructor(age) {
        super();
        this.age = age;
    }
    attack() {
        console.log('launch web')
    }
}

Comprehensions

names = [c.name for c in customers if c.admin]

(Experimental in Babel)

var names = [for (c of customers) if (c.admin) c.name];

What's better in Python

  • help(anything) :获得任何 module/method/function 的文档
  • 列表推理,类魔法方法!
  • 强大的 OOP
  • 庞大而一致的标准库,例如:string有38种有用的方法
  • 内置 字符串和数组切片.

What's better in Javascript

  • 内置 JSON 支持
  • NPM 包管理是一个杀手锏:简单快速,甩了 pip+virtualenv 几条街
  • 在浏览器中工作 :)