by @zhangbao(zhangbao) #0105

An overview of

What is a class array? The Lodash _. IsArrayLike (value) function must meet the following conditions:

  1. Is not a function
  2. There arelengthattribute
  3. andlengthThe property value is not greater thanNumber.MAX_SAFE_INTEGERThe 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).

_.isArrayLikeImplementation 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:

  1. value ! = null: This judgment uses! =Rather than! = =. Let the judgment result befalseHas two values:nullundefined. That is, if you go toisArrayLikeThe value passed in by the function isnullundefinedIf 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.

  1. typeof value ! == 'function'The value passed in cannot be a function, and if it is, it is not an array of classes.
  2. isLength(value.length): Checks the incoming value lastlengthProperties,isLength(value.length)The return value isisArrayLikeThe 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:

  1. 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

(after)