The difference between anxiety and fear is that fear is fear of being in the world, while anxiety is anxiety in front of me.

— Sartre, Being and Nothing

Star: Pocket-LoDash This is the 19th article to read the source code for LoDash

Gitbook will also be updated with the warehouse. Gitbook address: Pocket-Lodash

preface

We explained how to get data types in loDash source Code analysis, but in some environments, some new es6 objects get the same type as object objects, which makes it impossible to make a detailed distinction. For example in IE11, by the Object. The prototype. The toString get DataView Object types for [Object Object]. So in getTag, LoDash does some compatibility things for these objects.

Rely on

import baseGetTag from './baseGetTag.js'
Copy the code

Source Code Analysis of Lodash: The Acquisition of Data Types

Source code analysis

const dataViewTag = '[object DataView]'
const mapTag = '[object Map]'
const objectTag = '[object Object]'
const promiseTag = '[object Promise]'
const setTag = '[object Set]'
const weakMapTag = '[object WeakMap]'

/** Used to detect maps, sets, and weakmaps. */
const dataViewCtorString = `The ${DataView}`
const mapCtorString = `The ${Map}`
const promiseCtorString = `The ${Promise}`
const setCtorString = `The ${Set}`
const weakMapCtorString = `The ${WeakMap}`

let getTag = baseGetTag

// Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6.
if ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) ||
    (getTag(new Map) != mapTag) ||
    (getTag(Promise.resolve()) ! = promiseTag) || (getTag(new Set) != setTag) ||
    (getTag(new WeakMap) != weakMapTag)) {
  getTag = (value) = > {
    const result = baseGetTag(value)
    const Ctor = result == objectTag ? value.constructor : undefined
    const ctorString = Ctor ? `${Ctor}` : ' '

    if (ctorString) {
      switch (ctorString) {
        case dataViewCtorString: return dataViewTag
        case mapCtorString: return mapTag
        case promiseCtorString: return promiseTag
        case setCtorString: return setTag
        case weakMapCtorString: return weakMapTag
      }
    }
    return result
  }
}
Copy the code

GetTag source code is very simple, processing is DataView, Map, Set, Promise, WeakMap and other objects, the following key points to explain.

ToString method of the function

const dataViewCtorString = `The ${DataView}`
const mapCtorString = `The ${Map}`
const promiseCtorString = `The ${Promise}`
const setCtorString = `The ${Set}`
const weakMapCtorString = `The ${WeakMap}`
Copy the code

DataView() {[native code]} function DataView() {[native code]} Because that the call Object. The prototype. ToString returns in a certain environment [Object Object], and constructor toString the returned string, contains a constructor, can use this to distinguish.

Constructor fetch in an instance

 const Ctor = result == objectTag ? value.constructor : undefined
 const ctorString = Ctor ? `${Ctor}` : ' '
Copy the code

Each instance contains a constructor property that points to the instance’s constructor, which can then be compared by calling its toString method.

Promise.resolve

getTag(Promise.resolve()) ! = promiseTagCopy the code

In the condition judgment, the use of the Promise. The resolve (), the purpose is to get use to Promise Object, because the Promise was a function, if direct call Object. The prototype. ToString, Object Function is returned.

License

CC BY-NC-ND 4.0

Finally, all articles will be simultaneously sent to the wechat public account, welcome to pay attention to, welcome to comment:

Akik The opposite corner