JavaScript数据类型

JavaScript Data Types

本文关键字:数据类型 JavaScript      更新时间:2023-09-26

从我的理解JavaScript没有int 浮点,但只是一个数字类型格式化为双精度64位浮点值,但JavaScript也有类型数组,可以是许多类型,包括:Int32ArrayUint32ArrayFloat32Array

所以我的问题是:做类型数组下面只是使用数字类型与一些位操作包装函数,或者它实际上使用一些其他数据类型?如果他们确实使用了其他类型,那么是否可以通过包装类型数组来创建自己的intfloat类型呢?
所以我的问题是:做类型数组下面只是使用数字类型与一些位操作包装函数,或者它实际上使用一些其他数据类型?

类型化数组不使用number类型。例如,new Int32Array(10)将创建一个包含十个32位整数的数组。因此,它确实会为您的数组分配40字节的空间。

内部存储在数组中的任何整数将只占用32位(4字节)的空间。但是,在读取数据时,int将被强制转换为JavaScript number(原语,而不是对象-因此没有大写)。因此,无法将int类型读入JavaScript。

JavaScript number数据类型是双精度浮点数。因此,它可以很好地表示较小的数据类型。然而,它不能表示64位整数,因为它本身就是一个64位浮点数。这就是我们没有Int64ArrayUint64Array的原因。

如果它们确实使用其他类型,那么是否可以通过包装类型化数组来创建自己的intfloat类型呢?

是的,有可能。但是,您必须定义自己的加法、减法、强制转换等函数。例如,我将这样做:

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);