Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
97
rated 0 times [  100] [ 3]  / answers: 1 / hits: 15962  / 8 Years ago, thu, july 28, 2016, 12:00:00

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.


More From » javascript

 Answers
-2

OP here. Funny to see this on Stack Overflow :)


Before stepping through the behaviour, its important to clarify a few things:



  1. Number value and Number object (a = 3 vs a = new Number(3)) are very different. One is a primitive, the other is an object. You cannot assign attributes to primitives, but you can to objects.



  2. Coercion between the two is implicit.


    For example:


    (new Number(3) === 3)  // returns false
    (new Number(3) == 3) // returns true, as the '==' operator coerces
    (+new Number(3) === 3) // returns true, as the '+' operator coerces


  3. Every Expression has a return value. When the REPL reads and executes an expression, this is what it displays. The return values often don't mean what you think and imply things that just aren't true.




Ok, here we go.


Original


The pledge.


> function dis() { return this }
undefined
> five = dis.call(5)
[Number: 5]

Define a function dis and call it with 5. This will execute the function with 5 as the context (this). Here it is coerced from a Number value to a Number object. It is very important to note that were we in strict mode this would not have happened.


> five.wtf = 'potato'
'potato'
> five.wtf
'potato'

Now we set the attribute five.wtf to 'potato', and with five as an object, sure enough it accepts the Simple Assignment.


> five * 5
25
> five.wtf
'potato'

With five as an object, I ensure it can still perform simple arithmetic operations. It can. Do its attributes still stick? Yes.


The turn.


> five++
5
> five.wtf
undefined

Now we check five++. The trick with postfix increment is that the entire expression will evaluate against the original value and then increment the value. It looks like five is still five, but really the expression evaluated to five, then set five to 6.


Not only did five get set to 6, but it was coerced back into a Number value, and all attributes are lost. Since primitives cannot hold attributes, five.wtf is undefined.


> five.wtf = 'potato?'
'potato?'
> five.wtf
undefined

I again attempt to reassign an attribute wtf to five. The return value implies it sticks, but it in fact does not because five is a Number value, not a Number object. The expression evaluates to 'potato?', but when we check we see it was not assigned.


The prestige.


> five
6

Ever since the postfix increment, five has been 6.


[#61224] Tuesday, July 26, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
ryanulyssesb questions
Sat, Mar 20, 21, 00:00, 3 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
Mon, Mar 9, 20, 00:00, 4 Years ago
Sun, Jul 7, 19, 00:00, 5 Years ago
;