Writing in the front

I just graduated in June this year. I always wanted to spend time on the react source code, but I found I couldn’t get started. I gave up every time I couldn’t read it for long, and finally I didn’t understand anything. Accidentally see an article in Zhihu, do not recommend directly to see has iterated so much version of the framework source code, suggest that you can step by step to see, from some simple tool library source code, will not be discouraged to see the initiative of the source code, but also can take the opportunity to consolidate their own foundation. Therefore, I decided to start from Lodash, and I came up with the idea of writing this series of LoDash. On the one hand, it is self-motivation, and on the other hand, it can be sent out to learn with you. Welcome your criticism and comments.

I have never contacted LoDash before. I plan to learn how to use an API from official documents. I will try to implement an API by myself, and then compare it with the source code to analyze my shortcomings. I’m looking at v4.17.15 (the latest) source code, so let’s start with _. IsObject

The first time to write nuggets, we are more inclusive ~

_.isObject(value)

Check whether value is of Object type.

Returns true or false.

Their implementation

function isObject(value) {
    const type = typeof value
    if(type= = ='function') {
        return true
    }
    if (type= = ='object'&& value ! = null) {return true
    }
    return false
}
Copy the code

The source code

function isObject(value) {
    var type = typeof value
    returnvalue ! = null && (type= ='object' || type= ='function')}Copy the code

Analysis of the

Source code refining a lot. It is worth studying and re-examining.

Related knowledge points involved in the implementation process are:

  1. What are the data types of JS?

    Boolean, undefined, String, number, null, symbol and object

  2. What are the data types for which typeof returns object?

    Null, array, regexp, date

    Typeof NULL === ‘object’, but null is the basic data type

  3. Function is also a typeof object, but typeof results in ‘function’.

  4. Null, undefined equality, and strict equality?

undefined == undefined          // true
undefiend === undefined         // true
undefined == null               // true
null == null                    // true
null === null                   // false
Copy the code

_.isObjectLike(value)

Check if value is a class object. If a value is a class object, it should not be null, and the result after typeof is “object”.

Return value: true/false.

Their implementation

function isObjectLike(value) {
    returnvalue ! = null && typeof value ==='object'
}
Copy the code

The source code

function isObjectLike(value) {
    returnvalue ! = null && typeof value =='object'
}
Copy the code

Analysis of the

IsObject involves similar knowledge points, so the basic implementation.