简体   繁体   English

可变大小的JavaScript字符串如何是原始类型?

[英]How is the variable sized JavaScript string a primitive type?

From what I understand, the (basic) string type in JavaScript is a primitive type, meaning its variables are allocated on the stack. 据我了解,JavaScript中的(基本) string类型是原始类型,这意味着其变量分配在堆栈上。

I would have thought that for a type to be allocatable on the stack, it needed to have a fixed size -- something which presumably holds true for the other primitive types like boolean , number , etc. 我本来以为要在堆栈上分配一个类型,它必须具有固定的大小-这对于其他基本类型(如booleannumber等)也是如此。

Am I somehow wrong to assume that, or is some other internal magic used to make string s in JavaScript primitive types? 我以某种方式是错误的假设,还是在JavaScript基本类型中使用了其他一些内部魔术来制作string


EDIT: This gets more complicated when one considers that JavaScript is loosely typed. 编辑:当人们认为JavaScript是松散类型时,这变得更加复杂。 Which makes me wonder how any local variable can be allocated on the stack.... given that the size of what might be assigned to it during the course of a function is not fixed. 这让我想知道如何在堆栈上分配任何局部变量。...鉴于函数过程中可能分配给该变量的大小是不确定的。

But I guess (a perhaps simplified) answer to this might be that all local variables could be assigned a fixed maximum size on the stack. 但是我想(也许是简化的)答案可能是所有局部变量都可以在堆栈上分配一个固定的最大大小。 Say this is 8 bytes which I think is the size of the number type, and should be large enough to accommodate all the other primitive types (except the string ) as well as memory addresses (for when a local variable is assigned a reference type). 假设这是8个字节,我认为这是number类型的大小,并且应该足够大以容纳所有其他原始类型( string除外)以及内存地址(用于为局部变量分配引用类型时) 。 But, surely strings cannot be limited to 8 bytes (or any size for that matter). 但是,字符串肯定不能限制为8个字节(或任何大小)。 Which makes me conclude that strings (even the primitive type ones) are not (cannot be) assigned on the stack. 这使我得出结论,没有(不能)在堆栈上分配字符串(甚至是原始类型的字符串)。 And hence the term "Primitive type" in JavaScript is used to mean a "basic/building block" type, rather than one which is necessarily allocated on the stack (contradicting what I have read in numerous sources including the book "Professional JavaScript..." by Nicholas Zakas). 因此,JavaScript中的“原始类型”一词用于表示“基本/构建块”类型,而不是必须在堆栈上分配的类型(这与我在众多书籍(包括《专业JavaScript》)中所读到的内容相矛盾。 ”(尼古拉斯·扎卡斯)。

Anyone have any other take or a pointer to a good source talking about this? 有人对此有其他看法或指向好的消息来源的指针吗?

A string is a both an object and a primitive. 字符串既是对象又是基元。

When doing: 进行时:

var s = "this is a string";

you actually do: 您实际上是这样做的:

var s = new string("this is a string");

behind the curtains. 在窗帘后面。

The first being a primitive array with characters, on which the second one refers. 第一个是带有字符的原始数组,第二个是字符数组。

Strings are immutable, meaning they can't be changed. 字符串是不可变的,这意味着它们不能更改。 If you try to change it (ie reverse it), you will create a new string primitive, on which the object reference will point to. 如果尝试更改(即反转)它,则将创建一个新的字符串基元,对象引用将指向该基元。

用于在Javascript解释器中表示变量的存储不必看起来像堆栈-它取决于实现。

  1. Strings aren't allocated on the stack 字符串未分配在堆栈上
  2. where a variable is allocated doesn't distinguish primitives from objects 分配变量的位置无法区分原始对象和对象
  3. Strings aren't primitives, they are of the class "string" 字符串不是基元,它们属于“字符串”类

The difference between a primitive and a type is that types have methods and you can assign new properties to them: 基本类型和类型之间的区别在于类型具有方法,您可以为其分配新属性:

var a = 1, b = {}, s = '';
a.foo = 1; // doesn't work, but no error either
b.foo = 1; // works
s.foo = 1; // doesn't work, but no error either

console.log(a.foo);
console.log(b.foo);
console.log(s.foo);
​

gives

undefined
1
undefined

So all in all, I'm not sure that using "primitive" makes sense in JavaScript since the line is blurred. 因此,总之,由于行模糊,我不确定在JavaScript中使用“原始”是否有意义。

A string is a "value object" which means you can't change any of the properties. 字符串是“值对象”,这意味着您无法更改任何属性。 For example, when you replace characters in a string, you get a new string; 例如,当您替换字符串中的字符时,会得到一个新的字符串。 the old string doesn't change. 旧的字符串不变。

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

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