Javascript 字符串是原始类型?

标签 javascript string primitive-types

在 Javascript 中,数字、字符串和 bool 值被认为是原始类型。 原始类型通过副本传递。 好的,请考虑以下代码:

var s1 = "this is a string of 1000 characters ...";
var s2 = s1; // (2)

第 (2) 行发生了什么? 1000个字符被复制到变量s2? 或者是否存在一个内存位置并且 s1 和 s2 都引用该内存位置? 我相信第二个是正确的。 如果是这样,为什么所有的书都说字符串是原始类型,它们不是,它们是 引用类型,不是吗?

最佳答案

What happens in line (2)? 1000 characters are copied to the variable s2? OR is there one memory location and both s1 and s2 refer to this memory location?

这是 JavaScript 引擎的实现细节,无法从 JavaScript 程序内部看出区别。

why all books say that strings are primitive types

语言 defines them as such .

they are reference types, aren't they?

它们可能以低于 JS 的级别实现,但这对 JS 作者来说并不重要。

关于Javascript 字符串是原始类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22741418/

相关文章:

c - 如何强制预处理器在 C/C++ 中使用所需的数据类型?

javascript - 如何在 Web 应用程序中实现 Visual Code 智能感知

Javascript 迭代嵌套循环并返回特定值

javascript - 寻找三次贝塞尔曲线控制点的算法(实现细节)

java - 为什么我的 getter 应该返回对象数组时却返回 [LSubmarine;@27c170f0

PHP类型提示原始值?

javascript - 如何用 ;$39 替换所有出现的相同字符,例如 '

c - 将字符串字符转换为 int

c# - 如何将一串要点(带有标题和正文内容)拆分成一个多维数组?

java - Java中三元运算符中的char转换,打印int值而不是char值