JavaScript数据类型
JavaScript Data Types
从我的理解JavaScript没有int 或浮点,但只是一个数字类型格式化为双精度64位浮点值,但JavaScript也有类型数组,可以是许多类型,包括:Int32Array, Uint32Array和Float32Array。
所以我的问题是:做类型数组下面只是使用数字类型与一些位操作包装函数,或者它实际上使用一些其他数据类型?如果他们确实使用了其他类型,那么是否可以通过包装类型数组来创建自己的int和float类型呢?所以我的问题是:做类型数组下面只是使用数字类型与一些位操作包装函数,或者它实际上使用一些其他数据类型?
类型化数组不使用number
类型。例如,new Int32Array(10)
将创建一个包含十个32位整数的数组。因此,它确实会为您的数组分配40
字节的空间。
内部存储在数组中的任何整数将只占用32位(4字节)的空间。但是,在读取数据时,int将被强制转换为JavaScript number
(原语,而不是对象-因此没有大写)。因此,无法将int类型读入JavaScript。
JavaScript number
数据类型是双精度浮点数。因此,它可以很好地表示较小的数据类型。然而,它不能表示64位整数,因为它本身就是一个64位浮点数。这就是我们没有Int64Array
或Uint64Array
的原因。
如果它们确实使用其他类型,那么是否可以通过包装类型化数组来创建自己的int和float类型呢?
是的,有可能。但是,您必须定义自己的加法、减法、强制转换等函数。例如,我将这样做:
var Num = defclass({
constructor: function (array) {
this.constructor = function () {
this.array = new array(arguments);
};
return defclass(this);
},
toValue: function () {
return this.array[0];
},
toString: function () {
return this.array[0];
},
plus: function (that) {
return new this.constructor(this + that);
}
});
var Int8 = new Num(Int8Array);
var Uint8 = new Num(Uint8Array);
var Int16 = new Num(Int16Array);
var Uint16 = new Num(Uint16Array);
var Int32 = new Num(Int32Array);
var Uint32 = new Num(Uint32Array);
var Float32 = new Num(Float32Array);
var Float64 = new Num(Float64Array);
你可以这样使用:
var a = new Int32(Math.pow(2, 31) - 1); // 2147483647
var b = new Int32(1);
var c = a.plus(b); // -2147483648
defclass函数定义如下:
function defclass(prototype) {
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
all together:
var Num = defclass({
constructor: function (array) {
this.constructor = function () {
this.array = new array(arguments);
};
return defclass(this);
},
toValue: function () {
return this.array[0];
},
toString: function () {
return this.array[0];
},
plus: function (that) {
return new this.constructor(this + that);
}
});
var Int8 = new Num(Int8Array);
var Uint8 = new Num(Uint8Array);
var Int16 = new Num(Int16Array);
var Uint16 = new Num(Uint16Array);
var Int32 = new Num(Int32Array);
var Uint32 = new Num(Uint32Array);
var Float32 = new Num(Float32Array);
var Float64 = new Num(Float64Array);
var a = new Int32(Math.pow(2, 31) - 1); // 2147483647
var b = new Int32(1);
var c = a.plus(b); // -2147483648
alert(a + " + " + b + " = " + c);
function defclass(prototype) {
var constructor = prototype.constructor;
constructor.prototype = prototype;
return constructor;
}
JavaScript允许使用三种基本数据类型:
例如数字。123、120.50等
文本字符串,例如:"This text string"等
布尔值,如真或假。
JavaScript还定义了两个简单的数据类型,null和undefined,每一个都只定义一个值。
除了这些基本数据类型之外,JavaScript还支持一种称为对象的复合数据类型。
但是你可以像下面这样声明类型化数组:
// create an 8-byte ArrayBuffer
var b = new ArrayBuffer(8);
// create a view v1 referring to b, of type Int32, starting at
// the default byte index (0) and extending until the end of the buffer
var v1 = new Int32Array(b);
// create a view v2 referring to b, of type Uint8, starting at
// byte index 2 and extending until the end of the buffer
var v2 = new Uint8Array(b, 2);
// create a view v3 referring to b, of type Int16, starting at
// byte index 2 and having a length of 2
var v3 = new Int16Array(b, 2, 2);
- javascript解释器如何理解变量的数据类型
- 为什么不'我们在javascript中使用函数参数的数据类型
- 在javascript中调用函数/对象引用时,可容纳任何数据类型
- 在 Javascript 中更改数据类型
- 使用 ng-change 对输入类型数据进行 JavaScript 取消事件
- JavaScript中的“符号”原始数据类型是什么?
- 了解JavaScript中的基元数据类型
- 数值的 JavaScript 数据类型
- javascript中具有不同数据类型的数组
- 如何使用 javascript 获取剑道网格列的数据类型
- 如何判断javascript变量的数据类型是Array
- javascript中是否有字典数据类型
- 为什么要在javascript中使用数据类型
- 当变量的数据类型在 javascript 中为 'Null'
- 我什么时候应该在 JavaScript 中使用数据类型类
- 自定义数据类型,css和javascript
- 声明与 JavaScript 中存在的数据类型同名的类
- 什么是函数的数据类型:函数还是对象?在 JavaScript 中
- 如何从字符串 javascript 中获取数据类型
- 引用数据类型 JavaScript