var a=5; a占几个byte? ---谈谈Datatype

在一种程序设计语言中,能够表示并操作的值的类型称为数据类型(data type)。

//在Java中,函数不能被当作值操作,所以函数在Java不算是data type 

基本类型(fundamental datatypes):
5个primitive datatypes: { number, string ,Boolean, null and undefined }
composite datatype: {object}

基本类型的说明:
1. JavaScript also defines two trivial datatypes, null and undefined, each of which defines only a single value.

2. An object (that is, a member of the datatype object) represents a collection of values (either primitive values,
 such as numbers and strings, or composite values, such as other objects).

3. Objects in JavaScript have a dual nature: an object can represent an unordered collection of named values or an ordered collection of numbered values.
 In the latter case, the object is called an array. Although objects and arrays are fundamentally the same datatype in JavaScript,  they behave quite differently and will usually be considered distinct types throughout this book.

4. JavaScript defines another special kind of object, known as a function.
 A function is an object that has executable code associated with it.
 A function may be invoked to perform some kind of operation.
 Like arrays, functions behave differently from other kinds of objects,
 and JavaScript defines a special language syntax for working with them.
 Thus, we'll treat the function datatype independently of the object and array types.

5. In addition to functions and arrays, core JavaScript defines a few other specialized kinds of objects.
 These objects do not represent new datatypes, just new classes of objects.
 The Date class defines objects that represent dates, the RegExp class defines objects that represent regular expressions.

6. 简单看来,object > function、array、Date

------------
number:
All numbers in JavaScript are represented as floating-point values.
 JavaScript represents numbers using the 64-bit floating-point format defined by the IEEE 754 standard,
 which means it can represent numbers as large as ±1.7976931348623157 x 10308 and as small as ±5 x 10-324.
 
回到问题,var a=5; a占几个byte?
根据上面的分析可知,是8个byte(64-bit).
b=5.5,b也是8 byte。

而var obj=new Object(); obj是引用类型,obj本身占多少呢?我也不知道。

            
在ECMAScript中,变量可以存放两种类型的值,即原始值和引用值。
原始值(primitive value)是存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量访问的位置。
引用值(reference value)是存储在堆(heap)中的对象,也就是说,存储在变量处的值是一个指针(point),指向存储对象的内存处。

为变量赋值时,ECMAScript的解释程序必须判断该值是原始类型的,还是引用类型的。
要实现这一点,解释程序则需尝试判断该值是否为ECMAScript的原始类型之一,即Undefined、Null、Boolean和String型。
由于这些原始类型占据的空间是固定的,所以可将它们存储在较小的内存区域栈中。这样存储便于迅速查寻变量的值。

在许多语言中,字符串都被看作引用类型,而非原始类型,因为字符串的长度是可变的。ECMAScript打破了这一传统。

如果一个值是引用类型的,那么它的存储空间将从堆中分配。由于引用值的大小会改变,所以不能把它放在栈中,否则会降低变量查寻的速度。
相反,放在变量的栈空间中的值是该对象存储在堆中的地址。地址的大小是固定的,所以把它存储在栈中对变量性能无任何负面影响。

请大家指正!

 

本文大段内容copy自JavaScript: The Definitive Guide, 5th Edition 和 JavaScript高级程序设计

 

你可能感兴趣的:(JavaScript)