此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

DataView.prototype.getUint16()

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月⁩.

getUint16() 方法从 DataView 相对于起始位置偏移 n 个字节处开始,获取一个 16-bit 数 (无符号短整型,2 个字节)。

尝试一下

// Create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(16);

const view = new DataView(buffer);
view.setUint16(1, 65535); // Max unsigned 16-bit integer

console.log(view.getUint16(1));
// Expected output: 65535

语法

dataview.getUint16(byteOffset [, littleEndian])

参数

byteOffset

偏移量,单位为字节,从头开始计算。

littleEndian

可选 Indicates whether the 16-bit int is stored in little- or big-endian format. If false or undefined, a big-endian value is read.

返回

一个无符号短整型 16 位数。

抛出错误

RangeError

如果 byteOffset 超出了视图能储存的值,就会抛出错误。

描述

没有对齐约束; 多字节值可以从任何偏移量获取。

示例

js
var buffer = new ArrayBuffer(8);
var dataview = new DataView(buffer);
dataview.getUint16(1); // 0

规范

Specification
ECMAScript® 2026 Language Specification
# sec-dataview.prototype.getuint16

浏览器兼容性

参见