javascript - The Abstract Relational Comparison Algorithm
天蓬老师
天蓬老师 2017-04-10 14:59:28
0
0
953

11.8.5 The Abstract Relational Comparison Algorithm

The comparison x < y, where x and y are values, produces true, false, or undefined (which indicates that at least one operand is NaN). In addition to x and y the algorithm takes a Boolean flag named LeftFirst as a parameter. The flag is used to control the order in which operations with potentially visible side-effects are performed upon x and y. It is necessary because ECMAScript specifies left to right evaluation of expressions. The default value of LeftFirst is true and indicates that the x parameter corresponds to an expression that occurs to the left of the y parameter’s corresponding expression. If LeftFirst is false, the reverse is the case and operations must be performed upon y before x. Such a comparison is performed as follows:

  1. If the LeftFirst flag is true, then
    1. Let px be the result of calling ToPrimitive(x, hint Number).
    2. Let py be the result of calling ToPrimitive(y, hint Number).
  2. Else the order of evaluation needs to be reversed to preserve left to right evaluation
    1. Let py be the result of calling ToPrimitive(y, hint Number).
    2. Let px be the result of calling ToPrimitive(x, hint Number).
  3. If it is not the case that both Type(px) is String and Type(py) is String, then
    1. Let nx be the result of calling ToNumber(px). Because px and py are primitive values evaluation order is not important.
    2. Let ny be the result of calling ToNumber(py).
    3. If nx is NaN, return undefined.
    4. If ny is NaN, return undefined.
    5. If nx and ny are the same Number value, return false.
    6. If nx is +0 and ny is −0, return false.
    7. If nx is −0 and ny is +0, return false.
    8. If nx is +∞, return false.
    9. If ny is +∞, return true.
    10. If ny is −∞, return false.
    11. If nx is −∞, return true.
    12. If the mathematical value of nx is less than the mathematical value of ny —note that these mathematical values are both finite and not both zero—return true. Otherwise, return false.
  4. Else, both px and py are Strings
    1. If py is a prefix of px, return false. (A String value p is a prefix of String value q if q can be the result of concatenating p and some other String r. Note that any String is a prefix of itself, because r may be the empty String.)
    2. If px is a prefix of py, return true.
    3. Let k be the smallest nonnegative integer such that the character at position k within px is different from the character at position k within py. (There must be such a k, for neither String is a prefix of the other.)
    4. Let m be the integer that is the code unit value for the character at position k within px.
    5. Let n be the integer that is the code unit value for the character at position k within py.
    6. If m < n, return true. Otherwise, return false.

NOTE 1 Step 3 differs from step 7 in the algorithm for the addition operator + (11.6.1) in using and instead of or.

NOTE 2 The comparison of Strings uses a simple lexicographic ordering on sequences of code unit values. There is no attempt to use the more complex, semantically oriented definitions of character or string equality and collating order defined in the Unicode specification. Therefore String values that are canonically equal according to the Unicode standard could test as unequal. In effect this algorithm assumes that both Strings are already in normalised form. Also, note that for strings containing supplementary characters, lexicographic ordering on sequences of UTF-16 code unit values differs from that on sequences of code point values.

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

répondre à tous(0)
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!