A Simple js Problem

  javascript, question

clipboard.png

The result of printing is
clipboard.png
rather than
{name:’1222′}

First of all, the basic types have no attributes.

Like var a = 10; var b = “abc”;

The reason why you can access the a.fixed method and the b.substr method is that the JS engine wraps the basic type data once, for example, a wraps Number(a) and b wraps String(b), so that the number can use the fixed method and the string can use substr.

Adding attributes to numbers and strings will not take effect. a.name =’this is your name’ is actually adding nane attributes to the Number(a) object.
Every time the wrapper generates a new object, the new object does not exist name, so accessing a.name returns undefined. Log (a) just shows the value of a.