2017-07-21 6 views
0

これは_.initialためunderscore.jsのコードです:_nditialはUnderscore.jsにあるよりも詳細ですか?

// Returns everything but the last entry of the array. Especially useful on 
    // the arguments object. Passing **n** will return all the values in 
    // the array, excluding the last N. 
    _.initial = function(array, n, guard) { 
    return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n))); 
    }; 

次のようにそれほど冗長と同等になるように私には思える:そのような詳細について尋ねる

_.initial = function(array, n, guard) { 
    return slice.call(array, 0, Math.min(0, -(n == null || guard ? 1 : n))); 
    }; 

があるため場違いではありませんはできるだけ多くのバイトを保存しようとします。たとえば、次のコメントを参考にしてください。

// Save bytes in the minified (but not gzipped) version: 
    var ArrayProto = Array.prototype, ObjProto = Object.prototype, FuncProto = Function.prototype; 

もっと冗長なコードが必要な理由はありますか?

答えて

0

ここで、n0である極端なケースでは異なります。それはそれ以前には魅力的な問題のようでした。極値の場合は、collectionと等しいので、_.initial(collection, 0)を書くのは役に立たないでしょう(実際の配列を与える、または取る)。ただし、nを変更する必要のあるコードでは、n = 0の極端な場合があります。

関連する問題