by abuckenheimer · 9y ago · 0 comments
This confuses me quite a bit, there are nuanced differences between these ideas in the two languages but at the surface these are things that very much exist in both languages
arrow functions:
(x,y) => { x + y }
vs. lambda x,y: x + y
method shorthand definition: MyObj = {
foo(x,y) { return x + y }
bar(x,y) { return x * y }
}
vs. class MyObj:
foo(self, x, y): return x + y
bar(self, x, y): return x * y
spread operator: function add(x,y) {
return x + y
}
add(...[1,2])
vs. def add(x, y):
x + y
add(*[1,2])
destructuring: [a, b, ...rest] = [10, 20, 30, 40, 50];
vs. a, b, *rest = [10, 20, 30, 40, 50]
functional array methods: forEach(["Wampeter", "Foma", "Granfalloon"], print);
vs. list(map(print, ["Wampeter", "Foma", "Granfalloon"]))
async methods: function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function add1(x) {
var a = resolveAfter2Seconds(20);
var b = resolveAfter2Seconds(30);
return x + await a + await b;
}
add1(10).then(v => {
console.log(v); // prints 60 after 2 seconds.
});
vs. async def resolve_after_2_seconds(x):
await asyncio.sleep(2)
return x
async def add1(x):
a = resolve_after_2_seconds(20)
b = resolve_after_2_seconds(30)
return x + await a + await b
loop = asyncio.get_event_loop()
loop.run_until_complete(add1(10))
There's a lot of fun differences between the origins of these features and how they work but the sentence to me doesn't quite to the idea of "Leaving Python for JavaScript" justiceNo comments yet.