# getAllKeysIn
# Description
获取 object 所有可枚举属性以及原型链上的属性,包括可枚举的 symbol 以及原型链可枚举的 symbol
# Params
{Object} object
# Return
Array
# Depend
import getSymbolsIn from './getSymbolsIn.js'
# Code
function getAllKeysIn(object) {
  const result = []
  for (const key in object) {
    result.push(key)
  }
  if (!Array.isArray(object)) {
    result.push(...getSymbolsIn(object))
  }
  return result
}
# Analyze
- 使用 for...in拿到object除symbol外所有可枚举属性
- 判断 object是否为array,如果不是array,则使用getSymbolsIn拿到所有可遍历symbol包括原型链上
# Remark
# Example
function A () {
  ;[1,2,3,4,5].forEach(k => {
    Object.defineProperty(this, Symbol(k), {
      'enumerable': true,
      'value': k,
    })
  })
}
const b = new A()
b['test'] = 'test'
Object.defineProperty(b, Symbol(7), {
  'value': 7,
  'enumerable': true,
})
console.log(getAllKeysIn(b)) // [ 'test', Symbol(1), Symbol(2), Symbol(3), Symbol(4), Symbol(5), Symbol(7) ]
← getAllKeys getHolder →