在 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/