3

Inspired from a SO question I noticed that the snippets code console.logs this weirdly in the following scenario:

function hello() {
  console.log('hello this is', this);
}

hello.call(5);

which prints hello this is {}

In node, this returns: hello this is [Number: 5]

and in chrome, this prints: hello this is Number {5}

What gives?

5
  • This is sorta fixed in a update to the stack snippet console, that was written for this bug report. Sadly, updating the snippet library seems to be low priority.
    – Cerbrus
    Commented May 16, 2018 at 19:40
  • what is the desired/ expected output?
    – Ilene
    Commented May 16, 2018 at 21:01
  • Do node and chrome have the exact same version of the exact same javascript engine?
    – user1228
    Commented May 16, 2018 at 21:02
  • @Ilene anything more useful than {}. I would prefer [Number: 5] given that Objects are printed with [] in JS Commented May 16, 2018 at 21:21
  • You could always use console.log('hello this is', this.toString()) or console.log(```hello this is ${this}```) instead of this; then you'll get the default toString implementation (in this case, hello this is 5). But as @Cerbrus notes, the Stack Snippets code base hasn't been changed since... 2014? No, looks like they at least updated the Tidy code in April. Maybe they can roll in the latest from canon... Commented May 17, 2018 at 0:03

0

You must log in to answer this question.

Browse other questions tagged .