原始值与参考值 [英] Primitive value vs Reference value
问题描述
我读了一本名为面向Web开发人员的专业Javascript的书,它说:变量由参考值或原始值指定。参考值是存储在内存中的对象。然后它没有说明如何存储原始值。所以我猜它没有存储在内存中。基于此,当我有这样的脚本时:
I read a book called "Professional Javascript for web developer" and it says: "Variable is assigned by Reference value or Primitive Value. Reference values are objects stored in memory". And then it says nothing about how Primitive value is stored. So I guess it isn't stored in memory. Based on that, when I have a script like this:
var foo = 123;
Javascript如何记住 foo
变量以后使用?
How does Javascript remember the foo
variable for later use?
推荐答案
变量
可以保存两种值类型中的一种:原始值
或参考值
。
A variable
can hold one of two value types: primitive values
or reference values
.
-
原始值
是存储在堆栈上的数据。 -
原始值
直接存储在变量访问的位置。 -
参考值
是对象存储在堆。 -
参考值
存储在变量location是指向存储对象的内存中的位置的指针。 - 原始类型包括
未定义
,Null
,Boolean
,Number
或String
。
Primitive values
are data that are stored on the stack.Primitive value
is stored directly in the location that the variable accesses.Reference values
are objects that are stored in the heap.Reference value
stored in the variable location is a pointer to a location in memory where the object is stored.- Primitive types include
Undefined
,Null
,Boolean
,Number
, orString
.
基础:
对象是ag财产的格言。属性可以引用对象
或原语
。 基元是值
,它们没有属性。
Objects are aggregations of properties. A property can reference an object
or a primitive
. Primitives are values
, they have no properties.
已更新:
Updated:
JavaScript有6种原始数据类型:字符串,数字,布尔,空,未定义,符号(ES6中的新功能)。除了null和undefined之外,所有基元值都具有包围原始值的对象等价物,例如, String 对象包装字符串基元。所有原语都是不可变的。
JavaScript has 6 primitive data types: String, Number, Boolean, Null, Undefined, Symbol (new in ES6). With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable.
这篇关于原始值与参考值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!