您将如何向没有Java编程经验的人解释Javascript Typed Arrays? [英] How would you explain Javascript Typed Arrays to someone with no programming experience outside of Javascript?

查看:109
本文介绍了您将如何向没有Java编程经验的人解释Javascript Typed Arrays?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在与Canvas纠缠不清,并提出了一些有关基于Web的游戏的想法.因此,我最近遇到了Javascript Typed Arrays.我已经在 MDN 中做了一些阅读,无法理解我所发现的任何东西.似乎在大多数情况下,当有人在解释类型数组时,他们会使用类似于其他语言的类比,这超出了我的理解.

I have been messing with Canvas a lot lately, developing some ideas I have for a web-based game. As such I've recently run into Javascript Typed Arrays. I've done some reading for example at MDN and I just can't understand anything I'm finding. It seems most often, when someone is explaining Typed Arrays, they use analogies to other languages that are a little beyond my understanding.

我在编程"方面的经验(如果可以这样称呼它(而不仅仅是前端脚本))几乎仅限于Javascript.但是,我确实觉得我很了解Javascript.我已深入研究和使用的JavaScript的Object.prototype结构,更微妙的因素,如变量引用和的价值,但是当我看任何信息,我发现关于类型化数组,我只是失去了

My experience with "programming," if you can call it that (and not just front-end scripting), is pretty much limited to Javascript. I do feel as though I understand Javascript pretty well outside of this instance, however. I have deeply investigated and used the Object.prototype structure of Javascript, and more subtle factors such as variable referencing and the value of this, but when I look at any information I've found about Typed Arrays, I'm just lost.

牢记此参考框架,您能否以简单,可用的方式描述类型化数组?对我来说,最有效的用例是与Canvas图像数据有关.另外,最受好评的小提琴.

With this frame-of-reference in mind, can you describe Typed Arrays in a simple, usable way? The most effective depicted use-case, for me, would be something to do with Canvas image data. Also, a well-commented Fiddle would be most appreciated.

推荐答案

在类型化的编程语言(JavaScript kinda所属的语言)中,我们通常具有固定声明类型的变量,可以动态分配值.

In typed programming languages (to which JavaScript kinda belongs) we usually have variables of fixed declared type that can be dynamically assigned values.

Typed Arrays相反.

您有一个固定的数据块(由表示),您不能直接访问.而是通过视图访问此数据.视图是在运行时创建的,它们有效地将缓冲区的某些部分声明为某种类型.这些视图是ArrayBufferView的子类.视图将数据块的某些连续部分定义为某种类型的数组的元素.一旦声明了类型,浏览器就会知道每个元素的长度和内容,以及许多这样的元素.有了这些知识,浏览器可以更有效地访问各个元素.

You have a fixed chunk of data (represented by ArrayBuffer) that you do not access directly. Instead this data is accessed by views. Views are created at run time and they effectively declare some portion of the buffer to be of a certain type. These views are sub-classes of ArrayBufferView. The views define the certain continuous portion of this chunk of data as elements of an array of a certain type. Once the type is declared browser knows the length and content of each element, as well as a number of such elements. With this knowledge browsers can access individual elements much more efficiently.

因此,我们将类型动态分配给实际上只是缓冲区的一部分.我们可以将多个视图分配给同一缓冲区.

So we dynamically assigning a type to a portion of what actually is just a buffer. We can assign multiple views to the same buffer.

> 规范 :

Multiple typed array views can refer to the same ArrayBuffer, of different types,
lengths, and offsets. 

This allows for complex data structures to be built up in the ArrayBuffer.

作为示例,给出以下代码:

As an example, given the following code:

      // create an 8-byte ArrayBuffer
      var b = new ArrayBuffer(8);

      // create a view v1 referring to b, of type Int32, starting at
      // the default byte index (0) and extending until the end of the buffer
      var v1 = new Int32Array(b);

      // create a view v2 referring to b, of type Uint8, starting at
      // byte index 2 and extending until the end of the buffer
      var v2 = new Uint8Array(b, 2);

      // create a view v3 referring to b, of type Int16, starting at
      // byte index 2 and having a length of 2
      var v3 = new Int16Array(b, 2, 2);

The following buffer and view layout is created:

这定义了一个8字节的缓冲区b,以及该缓冲区v1的三个视图, v2和v3.每个视图都引用相同的缓冲区-因此v1 [0] 引用字节0..3作为有符号的32位整数,v2 [0]引用字节 2是无符号的8位整数,而v3 [0]将字节2..3引用为a 有符号的16位整数.对一个视图的任何修改都将立即生效 在另一个中可见:例如,在v2 [0] = 0xff之后; v2 1 = 0xff; 然后v3 [0] == -1(其中-1表示为0xffff).

This defines an 8-byte buffer b, and three views of that buffer, v1, v2, and v3. Each of the views refers to the same buffer -- so v1[0] refers to bytes 0..3 as a signed 32-bit integer, v2[0] refers to byte 2 as a unsigned 8-bit integer, and v3[0] refers to bytes 2..3 as a signed 16-bit integer. Any modification to one view is immediately visible in the other: for example, after v2[0] = 0xff; v21 = 0xff; then v3[0] == -1 (where -1 is represented as 0xffff).

因此,我们无需声明数据结构并用数据填充它们,而是将数据并用不同的数据类型覆盖.

So instead of declaring data structures and filling them with data, we take data and overlay it with different data types.

这篇关于您将如何向没有Java编程经验的人解释Javascript Typed Arrays?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆