1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 10:22:23 +00:00

docs(arraybuffer): edit ab2str

This commit is contained in:
ruanyf 2017-10-15 09:37:12 +08:00
parent 4640bed36a
commit 24e9149543

View File

@ -452,17 +452,7 @@ Float64Array.BYTES_PER_ELEMENT // 8
```javascript
// ArrayBuffer转为字符串参数为ArrayBuffer对象
function ab2str(buf) {
if (buf && buf.byteLength < 128000) { // limit max length avoid overhead
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
const bufView = new Uint16Array(buf);
const len = bufView.length;
const bstr = new Array(len);
for (let i = 0; i < len; i++) {
bstr[i] = String.fromCharCode.call(null, bufView[i]);
}
return bstr.join('');
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
// 字符串转为ArrayBuffer对象参数为字符串