使用Number对象保存属性并递增数字,此代码中发生了什么? [英] What’s happening in this code with Number objects holding properties and incrementing the number?
问题描述
它与 5
。这将执行带有 5
的函数作为上下文( this
)。这里它是从Number值强制转换为Number对象。请注意我们是否在严格模式 这一点非常重要不会发生。
> five.wtf ='土豆'
'土豆'
> five.wtf
'马铃薯'
现在我们设置属性 five.wtf
到'potato'
,并且有五个作为对象,当然它接受简单分配。
> 5 * 5
25
> five.wtf
'马铃薯'
5
作为一个对象,我确保它仍然可以执行简单的算术运算。它可以。它的属性还坚持吗?是的。
转弯。
>五个++
5
> five.wtf
undefined
现在我们检查 five ++
。 后缀增量的技巧是整个表达式将根据原始值进行评估然后增加值。看起来五
仍然是五,但实际上表达式评估为五,然后将五
设置为 6
。
不仅五
设置为 6
,但它被强制转换为Number值,并且所有属性都丢失了。由于基元不能保存属性, five.wtf
未定义。
> ; five.wtf ='土豆?'
'土豆?'
> five.wtf
undefined
我再次尝试重新分配属性 wtf
到五
。返回值意味着它坚持,但它实际上并不是因为五
是一个Number值,而不是Number对象。表达式的计算结果为'马铃薯?'
,但是当我们检查时,我们发现它没有被分配。
威望。
> 5
6
自从后缀增量后, 5
已 6
。
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.
OP here. Funny to see this on Stack Overflow :)
Before stepping through the behaviour, its important to clarify a few things:
Number value and Number object (
a = 3
vsa = 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.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
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.
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
.
这篇关于使用Number对象保存属性并递增数字,此代码中发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!