A simple js problem

clipboard.png


clipboard.png
instead of
{name:"1222"}

May.19,2022

first of all, the basic types have no attributes,

like var a = 10; var b = "abc";

you can access the a.fixed method, the b.substr method, because the JS engine wraps the basic type data, such as a wrapping Number (a) and b wrapping it as String (b), so that numbers can use the fixed method and strings can use substr.

adding attributes to numbers and strings doesn't work; a.name = 'this is your name' is actually adding nane attributes to the Number (a) object.
each wrapper produces a new object, and there is no name for the new object, so accessing a.name returns undefined. Console.log (a) just shows the value of a


because typeof a = number


js is a dynamically typed language
so types can change arbitrarily

so a becomes number type

instead of object type


javascript there are two data types:

  • value type (basic type): string ( String ), numerical value ( Number ), Boolean value ( Boolean ), Undefined , Null
  • reference type: object ( Object ), array ( Array ), function ( Function )

everything is an object in js , and the basic type is also an object in principle. The basic type can be used like an object type, through obj. Property name or obj [property name] can also be accessed through obj. Attribute name = value or obj [attribute name] = value to assign a value (although it doesn't actually work, but formally), so there is no syntax error, but it doesn't work.

as for your supplementary question, why doesn't an object defined by a work when you add a method to it later? Because in the second step, you reassign the value of a , making it equal to 10 again, and then it becomes Number type again, so it is invalid when you add the name method.

give an example:

  

a = 10
a.name = 'this is your name'
is equivalent to
10.name =' this is your name'
but 10 as a number, a basic type, there are no attributes available. Come out of it. It is just a value with no attribute

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eaeadf-47fc9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eaeadf-47fc9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?