A recent tweet contained this snippet of JavaScript.

Can someone please explain what is happening in it step by step?

> function dis() { return this }
undefined
> five = dis.call(5)
Number {[[PrimitiveValue]]: 5}
> five.wtf="potato"
"potato"
> five.wtf
"potato"
> five * 5
25
> five.wtf
"potato"
> five++
5
> five.wtf
undefined
> five.wtf="potato?"
"potato?"
> five.wtf
undefined
> five
6

In particular, it is not clear to me:

  • why the result of dis.call(5) is a Number with some kind of a [[PrimitiveValue]] property, but the results of five++ and five * 5 appear to just be the plain numbers 5 and 25 (not Numbers)
  • why the five.wtf property disappears after the five++ increment
  • why the five.wtf property is no longer even settable after the five++ increment, despite the five.wtf="potato?" assignment apparently setting the value.

10 Answers
10

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *