此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
DataView.prototype.setInt16()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
**setInt16()**从DataView 起始位置以 byte 为计数的指定偏移量 (byteOffset) 处储存一个 16-bit 数 (短整型)。
In this article
尝试一下
// Create an ArrayBuffer with a size in bytesconst buffer = new ArrayBuffer(16);const view = new DataView(buffer);view.setInt16(1, 32767); // Max signed 16-bit integerconsole.log(view.getInt16(1));// Expected output: 32767语法
dataview.setInt16(byteOffset, value [, littleEndian])
参数
- byteOffset
偏移量,从头开始计算,单位为字节。
- value
设置的数值。
- littleEndian
可选 Indicates whether the 16-bit int is stored inlittle- or big-endian format. If false or undefined, a big-endian value is written.
返回
抛出错误
RangeError如果 byteOffset 超出了视图能储存的值,就会抛出错误。
示例
js
var buffer = new ArrayBuffer(8);var dataview = new DataView(buffer);dataview.setInt16(1, 3);dataview.getInt16(1); // 3规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-dataview.prototype.setint16> |