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 aNumber
with some kind of a[[PrimitiveValue]]
property, but the results offive++
andfive * 5
appear to just be the plain numbers5
and25
(notNumber
s) - why the
five.wtf
property disappears after thefive++
increment - why the
five.wtf
property is no longer even settable after thefive++
increment, despite thefive.wtf = 'potato?'
assignment apparently setting the value.