by @zhangbao(zhangbao) #0105
An overview of
What is a class array? The Lodash _. IsArrayLike (value) function must meet the following conditions:
- Is not a function
- There are
length
attribute - and
length
The property value is not greater thanNumber.MAX_SAFE_INTEGER
The natural Numbers
A string fits this condition, which means it’s an array of classes.
The realization of the Lodash
Lodash provides a utility function: _. IsArrayLike (value), which checks whether a value is an array. Here’s the official use case:
_.isArrayLike([1.2.3]);
// => true
_.isArrayLike(document.body.children);
// => true
_.isArrayLike('abc');
// => true
_.isArrayLike(_.noop);
// => false
Copy the code
I took a peek at the source code for the implementation (here and here).
_.isArrayLike
Implementation details of
isArrayLike
Here is where the function isArrayLike is defined:
import isLength from './isLength.js'
function isArrayLike(value) {
returnvalue ! =null && typeofvalue ! = ='function' && isLength(value.length)
}
Copy the code
First check the input value:
value ! = null
: This judgment uses! =
Rather than! = =
. Let the judgment result befalse
Has two values:null
和undefined
. That is, if you go toisArrayLike
The value passed in by the function isnull
或undefined
If the first judgment is not satisfied, return directlyfalse
.
💡 tip: this introduces a small quirk of JS, which is defined in the specification for abstract equality algorithm (== internal execution of this algorithm) : null == undefined returns true, natural null! = undefined returns false.
typeof value ! == 'function'
The value passed in cannot be a function, and if it is, it is not an array of classes.isLength(value.length)
: Checks the incoming value lastlength
Properties,isLength(value.length)
The return value isisArrayLike
The return value of.
isLength
What’s the isLength function for? Let’s take a look:
const MAX_SAFE_INTEGER = 9007199254740991
/** * Checks if `value` is a valid array-like length. * * **Note:** This method is loosely based on * [` ToLength `] (http://ecma-international.org/ecma-262/7.0/#sec-tolength). * *... * * /
function isLength(value) {
return typeof value === 'number' &&
value > - 1 && value % 1= =0 && value <= MAX_SAFE_INTEGER
}
Copy the code
From the comments, we know that the isLength function is used to check whether the value passed in is a valid length. MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER = Number.
By the logic of judgment, we know that “valid length value” is a natural Number whose value is not greater than number. MAX_SAFE_INTEGER.
Values such as the following are not valid length values:
isLength(Number.MIN_VALUE)
/ / = > false. A negative number is not
isLength(Infinity)
/ / = > false. Infinity is not
isLength('3')
/ / = > false. Strings are not
Copy the code
Additional instructions
The isLength method is annotated as a loose implementation of ToLength, an abstract algorithm defined in the specification. Here is the definition in the specification:
The latter contains type conversion and parameter value correction logic compared to the definition in the specification:
- Type conversion: First convert the argument to an integer (
ToInteger(argument)
)
2. Parameter correction: The negative value is converted to+ 0
; Infinity goes to 2531, etc.
This is missing from the Lodash implementation, which is why it’s called a “loose implementation.”
Refer to the link
- Github.com/lodash/loda…
- Ecma-international.org/ecma-262/7….
(after)