JavaScript is almost pythonic
Tags: javascript, python
Published: 2017-12-05 01:22:08

---
title: JavaScript is almost pythonic
published: true
description: 
tags: JavaScript, Python
---

## Multi-line String

* Python3.6

```py
print("""string text line 1
string text line 2""")
```

* ES2017

```js
console.log(`string text line 1
string text line 2`)
```

## Expression Interpolation

* Python3.6

```py
a = 5
b = 10
print(f'Fifteen is {a + b} and not {2 * a + b}.')
```

* ES2017

```js
var a = 5
var b = 10
console.log(`Fifteen is ${a + b} and not ${2 * a + b}.`)
```

## Arrow function
* Python3.6

```python
numbers = [1, 2, 3, 4]

list(map(lambda x: x * 2, numbers))
# or [x * 2 for x in numbers]
```

* ES2017

```js
var numbers = [1, 2, 3, 4]
numbers.map(v => v * 2)
```

## Destructuring
* Python3.6

```py
numbers = (1, 2, 3)
x, y, z = numbers
```

* ES2017

```js
var numbers = [1, 2, 3]
var [x, y, z] = numbers
```

## Spread Operator
* Python3.6

```py
import datetime
date_fields = (2017, 12, 4)
date = datetime.date(*date_fields)

numbers = [1, 2, 3, 4]
first, *remaining = numbers

first = [1, 2]
second = [3, 4]
combined = first + second
```

* ES2017

```js
var dateFields = [2017, 12, 4]
var date = new Date(...dateFields)

var numbers = [1, 2, 3, 4]
var [first, ...remaining] = numbers

var first = [1, 2]
var second = [3, 4]
var combined = [...first, ...second]
```

## Rest Operator

* Python3.6

```py
from functools import reduce
def product(*numbers):
    return reduce(lambda x, y: x * y, numbers)

print(product(1, 2, 3, 4))
```

* ES2017

```js
function product(...numbers) {
    return numbers.reduce((x, y) => x * y)
}
console.log(product(1, 2, 3, 4))
```

## Default Parameter
* Python3.6

```py
def multiply(a, b=1):
	return a * b
```

* ES2017

```js
function multiply(a, b = 1) {
  return a * b
}
```

## Class
* Python3.6

```py
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return f"({self.x}, {self.y})"
```

* ES2017

```js
class Point {
    constructor(x, y) {
        this.x = x
        this.y = y
    }
    toString() {
        return `(${this.x}, ${this.y})`
    }
}
```

## Sub Class
* Python3.6

```py
class ColorPoint(Point):
    def __init__(self, x, y, color):
        super().__init__(x, y)
        self.color = color
    def __str__(self):
        return "{} in color {}".format(super().__str__(), self.color)
```

* ES2017

```js
class ColorPoint extends Point {
    constructor(x, y, color) {
        super(x, y)
        this.color = color
    }
    toString() {
        return `${super.toString()} in ${this.color}`
    }
}
```

## Getter & Setter
* Python3.6

```py
class SmartPoint(Point):
    @property
    def hypotenuse(self):
        return sqrt(self.x ** 2 + self.y ** 2)

    @hypotenuse.setter
    def hypotenuse(self, z):
        self.y = sqrt(z ** 2 - self.x ** 2)
```

* ES2017

```js
class SmartPoint extends Point {
    get hypotenuse() {
        return Math.sqrt(this.x ** 2 + this.y ** 2)
    }
    set hypotenuse(z) {
        this.y = Math.sqrt(z ** 2 - this.x ** 2)
    }
}
```

## Module
* Python3.6

```py
import math
print(math.log(42))

from math import log
print(log(42))

from math import *
print(log(42))
```

* ES2017

```js
import math from 'math'
console.log(math.log(42))

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

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

## Async Function
* Python3.6

```py
async def getProcessedData(url):
    try:
        v = await downloadData(url)
    except Exception:
        v = await downloadFallbackData(url)
    await processDataInWorker(v)
```

* ES2017

```js
async function getProcessedData(url) {
  let v
  try {
    v = await downloadData(url) 
  } catch (e) {
    v = await downloadFallbackData(url)
  }
  return processDataInWorker(v)
}
```

## Reference
* [JS is Becoming Pythonic](http://treyhunner.com/javascript-is-like-python/#/)
* [JavaScript Promises are equivalent to Python's asyncio](https://quentin.pradet.me/blog/javascript-promises-are-equivalent-to-pythons-asyncio.html)
* [JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript)