Why are methods of String.prototype available to string literals?

后端 未结 4 2079
北荒
北荒 2020-12-19 10:30

This question has come out of another, which concerns the behaviour of console.dir with string literals. In particular, see the comments on my answer.

A

4条回答
  •  我在风中等你
    2020-12-19 11:03

    Per the reference literals are converted to objects:

    String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives and String objects, so that it's possible to use String object methods for primitive strings.

提交回复
热议问题