目前工作中用到了Node.js, 于是看官方文档来学习一下。 在Buffer文档中, 关于使用Buffer.from, 我写了以下的测试代码:
> var arr = new Uint16Array(2)
undefined
> arr[0] = 5000
5000
> arr[1] = 4000
4000
> var buf1 = Buffer.from(arr)
undefined
> var buf2 = Buffer.from(arr.buffer)
undefined
> buf1
<Buffer 88 a0>
> buf2
<Buffer 88 13 a0 0f>
> arr[1] = 6000
6000
> buf1
<Buffer 88 a0>
> buf2
<Buffer 88 13 70 17>
问题如下:
使用arr和arr.buffer进行初始化, 区别是什么?
这个问题引申出以下问题:
为什么arr的修改会影响buf2而不影响buf1.
对于buf2来说, 1388 == 5000, 但buf1却只显示88?
1. The initialization problem of arr and arr.buffer. When using arr, a new storage space will be created.

When using arr.buffer, it only refers to the arr instance with the .buffer attribute and the newly created storage space. The storage space pointed to by arr is the same
2. After understanding the first question, the second question is very simple. buf1 points to a new storage space, and buf2 points to the same storage space as arr.
3. This is not clear. It can only be said that it is a phenomenon. buf1 outputs the first byte of data in the requested space, and buf2 outputs all bytes of data in the requested space. The results of the Uint32Array() I used here can illustrate.
I don’t know if the questioner understands question 3, can you tell me, thank you