开发者

Weird JavaScript bug(?) in Safari

开发者 https://www.devze.com 2023-02-01 23:26 出处:网络
I\'m seein开发者_C百科g an error with this code: $(\'#foo\').text(({rnd:((Math.random())>>0)}).rnd)

I'm seein开发者_C百科g an error with this code:

$('#foo').text(({rnd:((Math.random())>>0)}).rnd)

(Live copy)

In Safari 5.0 (5533.16), that very specific example shows a floating point number. Every other browser I've tested shows a zero (0).

My two questions are:

  1. Can you test this in whatever version of safari you have and tell me if it shows a zero or a random number.
  2. Anyone have any idea what could possibly be causing this?

My thoughts are that the >> is being somehow interpreted as (>)>0 instead of a right-shift.


My thoughts are that the >> is being somehow interpreted as (>)>0 instead of a right-shift.

No, if it were tokenizing it like that, it would see a syntax error and you wouldn't get anything at all. (And dozens of other things would break; the tokenizer has to be greedy on operators.)

More likely it's an erroneous optimization (now fixed, apparently) seeing the >> 0 as a no-op. I had to go double check the spec (Section 11.7.2 ["The Signed Right Shift Operator ( >> )"], page 76 of the 5th edition) to remind myself why it was coming out 0 as opposed to just the straight result of Math.random(). The key bit being, of course:

The result is a signed 32-bit integer.

Talk about your edge cases...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号