# isSymbol

# Description

判断是否为 Symbol 类型

# Params

value

# Return

Boolean

# Depend

    import getTag from './.internal/getTag.js'

getTag 源码分析

# Code

    function isSymbol(value) {
      const type = typeof value
      return type == 'symbol' || (type === 'object' && value != null && getTag(value) == '[object Symbol]')
    }

# Analyze

  1. 拿到 typeof value 的值
  2. typesymbol , 返回 true
  3. 当满足type == 'object',并且 value != null,同时 getTag(value) == '[object Symbol]'时,返回 true, 否则返回false

# Remark

    var sym = Symbol('foo')
    typeof sym // ‘symbol’
    var symbol_obj = Object(sym)
    typeof symbol_obj // ‘object’
    
    // 因此需要通过 getTag 方法来判断是否为 symbol

Symbol MDN (opens new window)

# Example

    isSymbol(Symbol.iterator) // true
    isSymbol(3) // false