简体   繁体   English

JavaScript数据类型

[英]JavaScript Data Types

From my understanding JavaScript doesn't have a ints or floats , but just a Number type which is formatted as a double-precision 64-bit floating point value, but JavaScript also has typed arrays which can be many types including: Int32Array , Uint32Array , and Float32Array . 根据我的理解,JavaScript没有整数浮点数 ,而只是一个数字类型,它被格式化为双精度64位浮点值,但JavaScript也有类型数组 ,可以是多种类型,包括: Int32ArrayUint32Array ,和Float32Array

So my question is: do typed arrays underneath just use the Number type with some bit operation wrapper functions, or does it actually use some other data type? 所以我的问题是:下面的类型化数组只使用带有一些位操作包装函数的Number类型,还是实际使用其他一些数据类型? And if they do use some other type, then is it possible to actually create your own int and float types by wrapping typed arrays . 如果它们确实使用了其他类型,那么是否可以通过包装类型化数组来实际创建自己的intfloat类型。

So my question is: do typed arrays underneath just use the Number type with some bit operation wrapper functions, or does it actually use some other data type? 所以我的问题是:下面的类型化数组只使用带有一些位操作包装函数的Number类型,还是实际使用其他一些数据类型?

Typed arrays do not use the number type. 键入的数组不使用number类型。 For example new Int32Array(10) will create an array of ten 32-bit ints. 例如, new Int32Array(10)将创建一个包含10个32位整数的数组。 Hence it would indeed allocate 40 bytes of space for your array. 因此它确实会为您的阵列分配40个字节的空间。

Internally any integer you store in the array will only occupy 32-bits (4-bytes) of space. 在内部,存储在数组中的任何整数只占用32位(4字节)的空间。 However when reading the data the int will be coerced into a JavaScript number (the primitive, not the object - hence not capitalized). 但是,在读取数据时,int将被强制转换为JavaScript number (原语,而不是对象 - 因此不会大写)。 Thus there's no way to read an int into JavaScript. 因此,没有办法将int读入JavaScript。

The JavaScript number data type is a double-precision floating point number. JavaScript number数据类型是双精度浮点数。 Hence it can represent smaller data types fine. 因此,它可以表示较小的数据类型。 However it cannot represent 64-bit integers, because it is itself a 64-bit floating point number. 但是它不能代表64位整数,因为它本身就是一个64位浮点数。 This is the reason we don't have a Int64Array or a Uint64Array . 这就是我们没有Int64ArrayUint64Array

And if they do use some other type, then is it possible to actually create your own int and float types by wrapping typed arrays . 如果它们确实使用了其他类型,那么是否可以通过包装类型化数组来实际创建自己的intfloat类型。

Yes, it is possible. 对的,这是可能的。 However you would have to define your own functions for addition, subtraction, coercion, etc. For example, this is what I would do: 但是你必须为加法,减法,强制等定义自己的函数。例如,这就是我要做的:

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

You can use it as follows: 您可以按如下方式使用它:

var a = new Int32(Math.pow(2, 31) - 1); // 2147483647
var b = new Int32(1);
var c = a.plus(b);                      // -2147483648

The defclass function is defined as follows: defclass函数定义如下:

function defclass(prototype) {
    var constructor = prototype.constructor;
    constructor.prototype = prototype;
    return constructor;
}

Everything put 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 allows you to work with three primitive data types: JavaScript允许您使用三种原始数据类型:

Numbers eg. 数字例如。 123, 120.50 etc. 123,120.50等

Strings of text eg "This text string" etc. 文本字符串,例如“此文本字符串”等。

Boolean eg true or false. 布尔值,例如true或false。

JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value. JavaScript还定义了两个简单的数据类型,null和undefined,每个数据类型只定义一个值。

In addition to these primitive data types, JavaScript supports a composite data type known as object. 除了这些原始数据类型之外,JavaScript还支持称为对象的复合数据类型。

but you can declare typed array like bellow: 但是你可以声明像下面这样的类型数组:

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

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

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