在 lodash 中,为什么谓词和结果变量没有以 var 关键字为前缀?
In lodash why are the predicate and results variables not prefixed with a var keyword?
这是 lodash 中 flattenDeep()
的一个副本,该函数将传递一个多维数组和 return 一个已展平的新数组。这个 flattenDeep()
是递归处理的。
阅读源代码我注意到:
predicate
和results
后面不用var吗?
predicate || (predicate = isFlattenable);
result || (result = []);
问题:为什么lodash对谓词和结果使用全局变量?这后面有reason/theory吗?
剥离出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));
它们不是全局的,任何传递给函数的参数都会被声明——这就是 var
所做的。
所以在这部分函数中:
function baseFlatten(array, depth, predicate, isStrict, result) {
var index = -1,
所有传入的参数前面基本上已经有一个var
,
并注意 index
确实有一个 var
,不会成为全球性的。
查看此快速 JSBin 并查看它通知您的错误...
这是 lodash 中 flattenDeep()
的一个副本,该函数将传递一个多维数组和 return 一个已展平的新数组。这个 flattenDeep()
是递归处理的。
阅读源代码我注意到:
predicate
和results
后面不用var吗?
predicate || (predicate = isFlattenable);
result || (result = []);
问题:为什么lodash对谓词和结果使用全局变量?这后面有reason/theory吗?
剥离出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));
它们不是全局的,任何传递给函数的参数都会被声明——这就是 var
所做的。
所以在这部分函数中:
function baseFlatten(array, depth, predicate, isStrict, result) {
var index = -1,
所有传入的参数前面基本上已经有一个var
,
并注意 index
确实有一个 var
,不会成为全球性的。
查看此快速 JSBin 并查看它通知您的错误...