# isSymbol
# Description
判断是否为 Symbol
类型
# Params
value
# Return
Boolean
# Depend
import getTag from './.internal/getTag.js'
# Code
function isSymbol(value) {
const type = typeof value
return type == 'symbol' || (type === 'object' && value != null && getTag(value) == '[object Symbol]')
}
# Analyze
- 拿到
typeof value
的值 type
为symbol
, 返回true
- 当满足
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
# Example
isSymbol(Symbol.iterator) // true
isSymbol(3) // false
← isString isTypedArray →