node-class-keys
Get keys of an instance of any class. Uses Object.getOwnPropertyNames
and Object.getOwnPropertySymbols
.
Usage:
npm i class-keys
const keys =
Why?
Because:
class Foo {
constructor () {
this.baz = 'baz'
}
bar () {}
[Symbol.for('foo')] () {}
}
const foo = new Foo()
Object.keys(foo) // []
Object.getOwnPropertyNames(foo) // []
Object.getOwnPropertySymbols(foo) // []
With class-keys
:
class Foo {
constructor () {
this.baz = 'baz'
}
bar () {}
[Symbol.for('foo')] () {}
}
const foo = new Foo()
classKeys(foo) // ['baz', 'constructor', 'bar', Symbol(foo)]
Warning
class-keys
uses instance's prototype to detect non-enumerable properties, so results may not match your expectation.
// [ '__defineGetter__', '__defineSetter__', 'hasOwnProperty', '__lookupGetter__', '__lookupSetter__', 'propertyIsEnumerable', 'constructor', 'toString', 'toLocaleString', 'valueOf', 'isPrototypeOf', '__proto__'] // ['constructor', 'toString', 'toDateString', 'toTimeString', ... 'toLocaleDateString', 'toLocaleTimeString', Symbol(Symbol.toPrimitive)]