DataView.prototype.setUint8()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Phương thức setUint8() của các instance DataView nhận một số và lưu trữ nó như một số nguyên không dấu 8-bit trong byte tại độ lệch byte được chỉ định của DataView này.

Try it

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

const view = new DataView(buffer);
view.setUint8(1, 255); // Max unsigned 8-bit integer

console.log(view.getUint8(1));
// Expected output: 255

Cú pháp

js
setUint8(byteOffset, value)

Tham số

byteOffset

Độ lệch, tính bằng byte, từ đầu view để lưu trữ dữ liệu vào.

value

Giá trị cần đặt. Để biết cách giá trị được mã hóa trong byte, xem Mã hóa và chuẩn hóa giá trị.

Giá trị trả về

undefined.

Ngoại lệ

RangeError

Được ném ra nếu byteOffset được đặt sao cho nó sẽ lưu trữ vượt quá cuối view.

Ví dụ

Sử dụng setUint8()

js
const buffer = new ArrayBuffer(10);
const dataview = new DataView(buffer);
dataview.setUint8(0, 3);
dataview.getUint8(0); // 3

Đặc tả kỹ thuật

Specification
ECMAScript® 2027 Language Specification
# sec-dataview.prototype.setuint8

Tương thích trình duyệt

Xem thêm