search

Home  >  Q&A  >  body text

javascript - Why can Array call methods directly, but ordinary Object objects cannot?

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?

phpcn_u1582phpcn_u15822732 days ago1372

reply all(3)I'll reply

  • 欧阳克

    欧阳克2017-07-05 11:07:29

    This is not the same as eval()parsing'{}'. The js parsing code will prioritize { as the same as the curly braces of function{}, 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

    8.toString()//报错
    (8).toString()// 正确
    8.0.toString()// 正确
    

    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 treats 8. as a number, In this case, it is certainly wrong to add . to toString.
    So it’s okay if it’s written like this

    8..toString()// 正确

    reply
    0
  • 学习ing

    学习ing2017-07-05 11:07:29

    First of all, your idea is wrong. Ordinary Object objects can call methods . For example

    var o = {};
    o.toString(); // OK

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-07-05 11:07:29

    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.

    var num = 1.2333;
    var flag = true;
                
    console.log({}.toString())   //[object, object]
    console.log(num.toFixed(2));  //1.23
    console.log(flag.toString()); //'true'   这里是字符串

    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.

    reply
    0
  • Cancelreply