javascript 如何在Javascript中声明一个字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11025414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare an array of byte in Javascript
提问by Gab
I am doing server-side javascript and i need to have a typed array of byte of a certain size. I tried :
我正在做服务器端 javascript,我需要有一个特定大小的字节类型数组。我试过 :
var buf = [1024]; (guives me Cannot convert org.mozilla.javascript.NativeArray@1e565bd to byte[] error)
var buf = byte[1024]; (wrong synthax)
What is the synthax?
合成器是什么?
采纳答案by severeon
This depends on which server-side JavaScript package you use. Different packages implement different flavors of JavaScript and different versions of ECMAScript.
这取决于您使用的服务器端 JavaScript 包。不同的包实现了不同风格的 JavaScript 和不同版本的 ECMAScript。
In NodeJS v0.6.x you have access to typed arrays. Creating one of these arrays is fairly trivial.
在 NodeJS v0.6.x 中,您可以访问类型化数组。创建这些数组之一是相当简单的。
// creating an array of bytes, with 1024 elements
var bytes = new Uint8Array(1024);
There are other typed arrays available, handling 16 bit and 32 bit integers.
还有其他类型的数组可用,处理 16 位和 32 位整数。
// creating an array of 16 bit integers, with 128 elements
var array_16bit = new Uint16Array(128);
// creating an array of 32 bit integers, with 16 elements
var array_32bit = new Uint32Array(16);
When using typed arrays, there are a few things to keep in mind. Typed arrays do not inherit the standard array prototype, and these arrays have an immutable length.
使用类型化数组时,需要记住一些事项。类型化数组不继承标准数组原型,并且这些数组具有不可变的长度。