简体   繁体   English

为什么string在JavaScript中是一个primitive?

[英]Why string is a primitive in JavaScript?

As far as I know objects can be of any size so you never know how big an object going to be.据我所知,对象可以是任何大小,所以你永远不知道 object 有多大。 So it was implemented as a reference type.所以它被实现为引用类型。 Unlike any primitive type which size you know in advance.That's what this article says.与您事先知道大小的任何原始类型不同。这就是本文所说的。 But strings... from another article:但是字符串......来自另一篇文章:

String: A set of “elements” of 16-bit unsigned integer values.字符串:一组 16 位无符号 integer 值的“元素”。 Because string is not a fixed value, the variable assigned a string does not contain the string but the reference to it.因为字符串不是固定值,所以分配给字符串的变量不包含字符串,而是对它的引用。

But how is string of a primitive type then?但是原始类型的字符串如何呢?

That book you're reading is weird.你正在读的那本书很奇怪。

A primitive type has a fixed size in memory. For example, a number occupies eight bytes of memory, and a boolean value can be represented with only one bit.原始类型的大小固定为 memory。例如,一个数字占用 8 个字节 memory,而一个 boolean 的值只能用一位来表示。 The number type is the largest of the primitive types.数字类型是最大的原始类型。 If each JavaScript variable reserves eight bytes of memory, the variable can directly hold any primitive value.如果每个JavaScript变量保留8个字节的memory,则该变量可以直接保存任何原始值。

Reference types are another matter, however.然而,引用类型是另一回事。 Objects, for example, can be of any length -- they do not have a fixed size.例如,对象可以是任意长度——它们没有固定的大小。 The same is true of arrays: an array can have any number of elements. arrays 也是如此:一个数组可以有任意数量的元素。 Similarly, a function can contain any amount of JavaScript code.同样,function 可以包含任意数量的 JavaScript 代码。 Since these types do not have a fixed size, their values cannot be stored directly in the eight bytes of memory associated with each variable.由于这些类型没有固定大小,因此它们的值不能直接存储在与每个变量关联的 memory 的八个字节中。 Instead, the variable stores a reference to the value.相反,变量存储对值的引用。 Typically, this reference is some form of pointer or memory address.通常,此引用是某种形式的指针或 memory 地址。 It is not the data value itself, but it tells the variable where to look to find the value.它不是数据值本身,而是告诉变量去哪里寻找值。

And then the author goes on to explain how this causes different behaviour, as if that was an effect of the memory layout.然后作者继续解释这是如何导致不同行为的,就好像这是 memory 布局的效果一样。 But they've got that the wrong way round.但他们的理解方式是错误的。 The distinction should be:区别应该是:

  • a primitive type has plain, immutable values, that will be copied by value when being passed around.基本类型具有简单的、不可变的值,在传递时将按值复制。
  • a reference type - synonymous with "the object type" - has mutable objects as values, which have an identity (created when the object is instantiated) and can have arbitrary changeable properties.引用类型 - 与“object 类型”同义 - 具有可变对象作为值,具有标识(在实例化 object 时创建)并且可以具有任意可变属性。 A variable never holds "an object" itself, but rather a reference to it, and it is this reference that is getting copied when you pass it around.一个变量从不持有“一个对象”本身,而是一个对它的引用,当你传递它时,这个引用就会被复制。

That (and "arrays and functions are objects") is the gist of the JavaScript value model. How this is implemented doesn't really matter - in fact it differs from engine to engine.那(和“数组和函数是对象”)是 JavaScript 值 model 的要点。这是如何实现的并不重要——事实上它因引擎而异。

Now when you ask " Why string is a primitive? ", the answer is "because strings are immutable values and don't have properties".现在当你问“为什么字符串是原始值? ”时,答案是“因为字符串是不可变的值并且没有属性”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM