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

14

u/Asmor Dec 16 '18

This won't work. Or at least, it didn't used to work. I haven't tried it in a very long time.

You'd need to do print = console.log.bind(console).

16

u/konaraddio Dec 16 '18

It works

I think you only need the .bind if console.log were to use this in its implementation

15

u/dvlsg Dec 17 '18

It used to need the bind. That was a recent-ish change, IIRC.

2

u/imguralbumbot Dec 17 '18

Hi, I'm a bot for linking direct images of albums with only 1 image

https://i.imgur.com/YMlhou8.png

Source | Why? | Creator | ignoreme | deletthis

0

u/[deleted] Dec 17 '18

[deleted]

2

u/Asmor Dec 17 '18

Honestly, it's even better to just not do this in the first place. If you're gonna screw with it, might as well wrap it in a function. That also gives you some extra control, like having a variable for debug level and silently eating logs below the level you care about.

0

u/[deleted] Dec 16 '18

[deleted]

5

u/cyphern Dec 17 '18 edited Dec 17 '18

Should be fine on modern javascript engines, but older ones can still have a problem with it. I just ran into a bug with this recently at work. I was porting some code over to a smart tv, and it threw an exception. The cause was an un-bound console.log (and console.warn and console.error) function.

3

u/bart2019 Dec 17 '18

And with browsers' tendency of constantly updating... Who cares about old browsers?

Especially since this is functionality for yourself, not for random visitors to your site.