ECMAScript 6(ES6) 特性概覽和與ES5的比較16-Typed Array

十六.Typed Array

Typed Array

支持基於任意字節的數據結構,以實現網絡協議,加密算法,文件格式操作等。

ECMAScript 6

//ES6類相當於一下C結構
//例如:struct Example { unsigned long id; char username[16]; float amountDue }
class Example {
    constructor (buffer = new ArrayBuffer(24)) {
        this.buffer = buffer
    }
    set buffer (buffer) {
        this._buffer = buffer
        this._id = new Unit32Array (this.buffer, 0, 1)
        this._username = new Unit8Array (this.buffer, 4, 16)
        this._amountDue = new Float32Array (this._buffer, 20, 1)
    }
    get buffer () { return this._buffer }
    set id (v) { this._id[0] = v }
    get id () { return this._id[0] }
    set username (v) { this._username[0] = v }
    get username () { return this._username[0] }
    set amountDue (v) { this._amountDue[0] = v }
    get amountDue () {return this._amountDue[0] }
}

let example = new Example()
example.id = 7
example.username = "John Doe"
example.amountDue = 42.0

ECMAScript 5

//ES5中沒有相應表達
//  (only an equivalent in HTML5)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章