2016-05-10 31 views
1

Hier ist eine Kopie von flattenDeep() in lodash, eine Funktion, die ein mehrdimensionales Array übergeben und ein neues Array zurückgeben, das abgeflacht wurde. Diese flattenDeep() wird rekursiv behandelt.In lodash, warum sind die Prädikats- und Ergebnisvariablen nicht mit einem var Schlüsselwort vorangestellt?

Lesen des Quellcodes Ich bemerkte, dass:

predicate und results verwenden var nicht hinter ihnen?

 predicate || (predicate = isFlattenable); 
     result || (result = []); 

Frage: Warum lodash globale Variablen für Prädikat und Ergebnisse verwendet haben? Gibt es einen Grund/eine Theorie dahinter?

Full src

gezupft js:

var bigarray = [[1],[2,[3,3]],[1], 1]; 


/** 
* Checks if `value` is a flattenable `arguments` object or array. 
* 
* @private 
* @param {*} value The value to check. 
* @returns {boolean} Returns `true` if `value` is flattenable, else `false`. 
*/ 
function isFlattenable(value) { 
    return Array.isArray(value); 
} 

/** 
* The base implementation of `_.flatten` with support for restricting flattening. 
* 
* @private 
* @param {Array} array The array to flatten. 
* @param {number} depth The maximum recursion depth. 
* @param {boolean} [predicate=isFlattenable] The function invoked per iteration. 
* @param {boolean} [isStrict] Restrict to values that pass `predicate` checks. 
* @param {Array} [result=[]] The initial result value. 
* @returns {Array} Returns the new flattened array. 
*/ 
function baseFlatten(array, depth, predicate, isStrict, result) { 
    var index = -1, 
     length = array.length; 

    predicate || (predicate = isFlattenable); 
    result || (result = []); 

    while (++index < length) { 
    var value = array[index]; 
    if (depth > 0 && predicate(value)) { 
     if (depth > 1) { 
     // Recursively flatten arrays (susceptible to call stack limits). 
     baseFlatten(value, depth - 1, predicate, isStrict, result); 
     } else { 
     arrayPush(result, value); 
     } 
    } else if (!isStrict) { 
     result[result.length] = value; 
    } 
    } 
    return result; 
} 


/** 
* Recursively flattens `array`. 
* 
* @static 
* @memberOf _ 
* @since 3.0.0 
* @category Array 
* @param {Array} array The array to flatten. 
* @returns {Array} Returns the new flattened array. 
* @example 
* 
* _.flattenDeep([1, [2, [3, [4]], 5]]); 
* // => [1, 2, 3, 4, 5] 
*/ 
function flattenDeep(array) { 
    var length = array ? array.length : 0; 
    return length ? baseFlatten(array, Infinity) : []; 
} 

console.log(flattenDeep(bigarray)); 

Antwort

1

Sie nicht global sind, werden alle Parameter, die in eine Funktion übergeben werden, werden erklärt - das ist, was var tut.

So in diesem Teil der Funktion:

function baseFlatten(array, depth, predicate, isStrict, result) { 
    var index = -1, 

Alle die im Wesentlichen in übergebenen Parameter bereits eine var vor ihnen haben, und beachten Sie, dass index in der Tat eine var hat, als nicht geworden global.

Sie dieser schnelle JSBin und die Fehler sehen, dass es Ihnen ... Sinn

https://jsbin.com/kebekib/3/edit?js,console

+0

Make benachrichtigt> _ <; Das war eine dumme Frage. –

+0

nicht dumm ... schwitz es nicht! – JordanHendrix