1
4

1 回答 1

1

The String.fromCharCode() argument is masked to an 16-bit unsigned integer, and this is in accordance with the ECMAScript standard. Thus, when executing String.fromCharCode(123456454545789), the argument is first reduced to 20861. This happens for any argument of the form 20861 + n*65536, so getting the original large number back is impossible (or, to put it formally, you can get an infinite number of such numbers).

This has nothing to do with surrogate code points. The string '兽' consists of one BMP character only. If you have non-BMP character (code number > 0xFFFF), say '' (U+20000), then surrogates are relevant, and for such a string (consisting of a single Unicode character internally represented in JavaScript as two surrogate code points), fromCharCode(0) and fromCharCode(1) are defined and yield the surrogate code point values, such as 55360 and 56320. (Modern implementations can usually deal with a literal containing a non-BMP character, like '', though this is not required in the standard.)

于 2012-04-11T10:46:26.633 回答