你能解释一下我不懂的这个功能以及如何在“普通”功能中转换它吗?

时间:2016-03-11 18:11:23

标签: javascript

我在互联网上找到了这个javascript函数,我不明白它是如何工作的。

  (function(o) {
Number.getOrdinalFor = function(intNum, includeNumber) {
  return (includeNumber ? intNum : '') + (o[((intNum = Math.abs(intNum % 100)) - 20) % 10] || o[intNum] || 'th');
};
 })([, 'st', 'nd', 'rd']);

我可以将它转换为正常的函数,如:

function getOrdinalFor (intNum, includeNumber) {
  // code
}

1 个答案:

答案 0 :(得分:2)

代码返回给定数字的序数,例如

Number.getOrdinalFor(1)       // returns "st"
Number.getOrdinalFor(2)       // returns "nd"
Number.getOrdinalFor(3)       // returns "rd"
Number.getOrdinalFor(4, true) // returns "4th"
                   //     ^ include number in output

将其转换为可以像

一样调用的常规函数
getOrdinalFor(1);

就像

一样简单
function getOrdinalFor(intNum, includeNumber) {
    var o = [, 'st', 'nd', 'rd'];
    return (includeNumber ? intNum : '') + (o[((intNum = Math.abs(intNum % 100)) - 20) % 10] || o[intNum] || 'th');
}

更具可读性,有评论

function getOrdinalFor(intNum, includeNumber) {
    var ordinals = [, 'st', 'nd', 'rd'];        // the ordinals, except "th", in an array, 
                                                // with the first index starting at 1

    var start    = includeNumber ? intNum : ''; // start with the number, or empty string
                                                // depending on includeNumber argument

    intNum     = Math.abs(intNum % 100);        // set intNum to the result of "modulus 100" on it self
                                                // this shortens down hundreds, thousands, and bigger
                                                // numbers to max two digits. For instance 
                                                // 2 = 2
                                                // 12 = 12
                                                // 233 = 33
                                                // 3444 = 44
                                                // 111111111 = 11
                                                // ...etc

    var calc   = (intNum - 20) % 10;            // then subtract 20, and do "modulus" 10
                                                // this shortens it to one integer, 
                                                // positive or negative, for instance
                                                // 2 = -8
                                                // 12 = -8
                                                // 33 = 3
                                                // 44 = 4
                                                // 11 = -9
                                                // ...etc


    var result = ordinals[calc] || ordinals[intNum] || 'th';  // start at the left hand, 
                                                              // and return first truthy value

    // for instance, using the results of the numbers above
    // ordinals[-8] does not exist (falsy), move along, ordinals[2] is "nd", so return "2nd"
    // ordinals[-8] does not exist (falsy), move along, ordinals[12] is falsy as well, so return "12th"
    // ordinals[3] is truthy, so return "33rd"
    // ...etc

    return result;
}