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)

猜你喜欢

转载自blog.csdn.net/u010622874/article/details/84068949
今日推荐