For example, we can sort the array like this:
[1,2,3].sort()
But you cannot call the object's method like this:
{}.toString()
Also, why can string methods be called directly, but Number types and Boolean types cannot.
I know something about strings. When calling the string method, a temporary String object will be generated. Why can't Number and Boolean types work?
This is not the same as
eval()
parsing'{}'
. The js parsing code will prioritize{
as the same as the curly braces offunction{}
, and will parse{
( start), in parsing}
(end). In this case, of course an error will be reported.And what should
eval()
do when parsing'{}'
? Add a bracket'({})'
; so that{}
will be parsed as a whole. At this time, it It’s the object.As for "Why strings can call methods directly, but Number and Boolean types cannot"
Boolean types can,
As for numbers, methods cannot be called directly, that is also the reason for js parsing, such as
Then why, because there are no real integers in js, integers are represented by floating point numbers, so when js parses
8
, it finds that there is.
after it, so it treats8.
as a number, In this case, it is certainly wrong to add.
totoString
.So it’s okay if it’s written like this
First of all, your idea is wrong. Ordinary Object objects can call methods . For example
But there is a syntax error in
{}.toString()
, because the{}
in{}.toString()
will be regarded as a statement block instead of an object direct quantity.Because JavaScript will parse
{}.toString()
from left to right, when it encounters{
, it will be regarded as the beginning of a statement block. After encountering}
, the statement block ends. A syntax error will occur when reaching.
.And if you use
({}).toString()
it will work normally. (Note the brackets surrounding{}
).Because when parsing
({}).toString()
, First encounter(
, then regards the part inside the brackets as an expression , and gets an empty object , so it is legal to perform method calls on this empty object.About
{}.toString()
, someone has already made it very clear. This is because the js engine will treat {} as a block mark when parsing. However, I feel that it is very strange. I have tried several browsers on my Mac and there is no problem with the problem mentioned by the poster.What I would like to add is the second question of the original poster,
Why string methods can be called directly, but Number and Boolean types cannot.
I think you must have made a mistake somewhere.Here, num is a numerical literal and flag is a Boolean value. Both of them can call methods. If the author knows something about strings, he should understand that basic types of data do not have attributes and methods, only objects have them.
But why can we call methods of basic data types just like calling methods of objects? (eg: 'a bc'.trim())
This is because when a method is called on a value of a basic data type, js will convert it into a temporary wrapper object. String literals can call properties and methods like string objects (new String('abc')), and numeric literals can call properties and methods like numeric objects.
Of course, this temporary packaging object only exists at the moment when the code is executed. After the method is executed, the temporary object is destroyed immediately.
So what the poster said is that it is impossible to call methods on Number and Boolean type values.