dataview中写入对象
DataView
is a view into an ArrayBuffer, like Typed Arrays, but in this case the items in the array can have different sizes and types.
DataView
是ArrayBuffer的视图,就像Typed Arrays一样 ,但是在这种情况下,数组中的项目可以具有不同的大小和类型。
Here’s an example:
这是一个例子:
const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
Since this is a view over a buffer, we can specify which byte we want to start from, and the length:
由于这是一个缓冲区视图,因此我们可以指定要从哪个字节开始以及长度:
const view = new DataView(buffer, 10) //start at byte 10
const view = new DataView(buffer, 10, 30) //start at byte 10, and add 30 items
If we don’t add those additional arguments, the view starts at position 0 and loads all the bytes present in the buffer.
如果不添加这些其他参数,则视图从位置0开始,并加载缓冲区中存在的所有字节。
There is a set of methods we can use to add data into the buffer:
我们可以使用一组方法将数据添加到缓冲区中:
setInt8()
setInt8()
setInt16()
setInt16()
setInt32()
setInt32()
setUint8()
setUint8()
setUint16()
setUint16()
setUint32()
setUint32()
setFloat32()
setFloat32()
setFloat64()
setFloat64()
This is how to call one of those methods:
这是调用这些方法之一的方法:
const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019)
By default data is stored using big endian notation. You can overwrite this setting and use little endian by adding a third parameter with the true
value:
默认情况下,数据使用大尾数表示法存储。 您可以通过添加第三个参数为true
值来覆盖此设置并使用little endian:
const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019, true)
Here is how we can get data from the view:
这是我们如何从视图中获取数据的方法:
getInt8()
getInt8()
getInt16()
getInt16()
getInt32()
getInt32()
getUint8()
getUint8()
getUint16()
getUint16()
getUint32()
getUint32()
getFloat32()
getFloat32()
getFloat64()
getFloat64()
Example:
例:
const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019)
view.getInt16(0) //2019
Since a DataView
is an ArrayBufferView
, we have those 3 read-only properties:
由于DataView
是ArrayBufferView
,因此我们具有这3个只读属性:
buffer
points to the original ArrayBufferbuffer
指向原始ArrayBufferbyteOffset
is the offset on that bufferbyteOffset
是该缓冲区的偏移量byteLength
is the length of its content in bytesbyteLength
是其内容的长度(以字节为单位)
One thing to keep in mind is that typed arrays don’t let us control the endianness: it uses the endianness of the system. In general this works out fine, because the main use case as we said is to use the array locally, using one of the multimedia APIs.
要记住的一件事是类型数组不允许我们控制字节序: 它使用系统的字节序 。 总的来说,这很好,因为我们所说的主要用例是使用多媒体API之一在本地使用数组。
If you transfer the data of a Typed Array on another system, the data might be not badly encoded if it uses Big Endian and you use Little Endian.
如果您在另一个系统上传输类型数组的数据,则使用Big Endian并使用Little Endian的数据可能编码不正确。
In case you need this kind of control, DataView is a perfect choice.
如果您需要这种控件, DataView是一个完美的选择。
dataview中写入对象