小写字符串是javascript中的原始数据类型。
用这种类型创建的字符串不是对象,但 javascript 会自动用 string 对象包装它们(这称为“装箱”)。
let imastring = "hello"; console.log(typeof imastring); // "string"
大写字符串 是一个构造函数,用于创建 string 对象,即字符串基元的对象包装器。
当您将 string 构造函数与 new 一起使用时,您将得到一个 string 对象而不是原始字符串
字符串对象不是必需的,除非您需要明确地将它们用作对象。
let imAStringObject = new String("hello"); console.log(typeof imAStringObject); // "object"
string | string | |
---|---|---|
type | primitive | object |
memory | lightweight and stored by value | heavyweight, stored as object |
methods | get converted to string object temporarily | has access to string methods like .charat() |
comparing values | by values | by reference |
几乎在所有情况下都使用字符串(原始)。它更高效、更简单,并且 javascript 在需要时自动提供方法。
仅当您特别需要具有附加属性的对象或想要使用 instanceof 检查时才使用 string(对象),尽管这在实践中很少见。
就是这样!感谢您阅读本文。下次见!
以上就是字符串与字符串的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号