javascript - javascript 中的字符串

标签 javascript string primitive

我最近看到一些引用资料将Javascript中的字符串解释为原始类型。我知道原始类型是一种数据类型,它不由其他数据类型组成,不能进一步分解。但问题是我也读过字符串是对象。它怎么可能是两者?请澄清我的困惑。

最佳答案

您可以在 MDN 上阅读有关该确切主题的信息:

Note that JavaScript distinguishes between String objects and primitive string values. (The same is true of booleans and numbers.)

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 to String objects, so that it's possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

关于javascript - javascript 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167045/

相关文章:

c - 返回类型为空。我的职能也需要帮助

python - 为什么我无法实现字符串的解码功能?

java - 在 Bundle 中,如何找出给定键值的类型?

C#正确高效的 double 到十进制的转换

javascript - 使用填充时动态更改 SVG 动画值 ="freeze"

mysql - 我可以使用 ColdFusion 查询 MySQL IN 语句中使用的字符串参数吗?

javascript - 关于插入到 DOM 中的 Javascript 对象的说明?

Java char 数组似乎每个 char 需要超过 2 个字节

javascript - 内容更改时的 Angular ng-disable 按钮

javascript - 如何根据单独元素的事件类更新元素类?