r/javascript • • Dec 16 '18

Coming from Python to JS NSFW

const print = console.log

I found my safe space.

607 Upvotes

156 comments sorted by

View all comments

84

u/uberpwnzorz Dec 17 '18

you need to use bind so the log function keeps the context of console.

const print = console.log.bind(console)

6

u/jetpacmonkey Dec 17 '18

Is this still true in every browser? I've noticed in Chrome that I can do promise.then(console.log), and I'm pretty sure that didn't work a few years ago.

10

u/LiverReich Dec 17 '18

This comment needs a lot more appreciation

5

u/username_is_taken43 Dec 17 '18

Yeah, don't hide the beauty of Javascript

1

u/bart2019 Dec 17 '18

Except, console doesn't care, afaik.

7

u/theXpanther Dec 17 '18

It does, there was a stack overflow question about someone forgetting to bind

6

u/maple3142 Dec 17 '18

I think it depends on your javascript engine version. http://www.reddit.com/r/javascript/comments/a6txtp/-/eby144i

3

u/minuskruste Dec 17 '18

It depends on the context. It also won’t throw an error but it won’t work in some cases either. Particularly if you pass console.log as a function to a tap in an observable pipe.

0

u/[deleted] Dec 17 '18

[deleted]

2

u/uberpwnzorz Dec 18 '18

it's a prototype function, technically. MDN calls it a function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

Classes and Methods are just sugar for prototype in JavaScript

1

u/saadq_ Dec 17 '18

A method is a function though