简体   繁体   English

是(new Uint8Array(1))[0] = 0x100未定义的结果?

[英]Is the result of (new Uint8Array(1))[0] = 0x100 undefined?

Looking at the spec , the following behavior doesn't seem to be defined clearly : 查看规范 ,似乎没有明确定义以下行为:

var array = new Uint8Array( 1 );
var result = ( array[ 0 ] = 0x100 );

// Is result === 0x100 or 0 ?

Chrome is returning 0x100 instead of 0 , but can I trust this result to be consistent ? Chrome正在返回0x100而不是0 ,但是我可以相信这个结果是一致的吗?

What you are doing here: 你在这做什么:

var result = ( array[ 0 ] = 0x100 );

is actually the same (a short-hand) as setting both array and result with the same value at the same time (the parenthesis does not matter as 0x100 is the one being evaluated and passed to result): 实际上是同一个(简写)同时设置数组和结果具有相同的值(括号无关紧要,因为0x100是被评估并传递给结果的那个):

var result = array[ 0 ] = 0x100;

or expanded: 或扩大:

array[ 0 ] = 0x100;
var result = 0x100;

so obviously result would in this case be 0x100 (256). 所以很明显,在这种情况下, result将是0x100(256)。

But the content of the array is 0 as you can see if you log it directly: 但是如果你直接记录它,你可以看到数组的内容为0:

console.log(array[ 0 ]);

(if you used Uint8ClampedArray instead the value would be 0xff or 255). (如果您使用Uint8ClampedArray则值将为0xff或255)。

Fiddle 小提琴

So in that case the result will be consistent but by-passing the array in relation to the result variable and the value set. 因此,在这种情况下,结果将是一致的,但是相对于结果变量和值集绕过数组。 Array will be set too, but the value is fit to the unsigned byte range while the value is as-is for the result var. 也将设置数组,但该值适合无符号字节范围,而值为结果var的原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM