get an object's list of owned keys (string keys and symbol keys).

Note

owned keys of an object are not the same as just any key of the object. an owned key is one that it directly owned by the object, not owned through inheritance, such as the class methods. more precisely, in javascript, which ever member of an object that we call a property, is an owned key.

if you wish to acquire all remaining inherited keys of an object, you will want use getInheritedPropertyKeys.

import { assertEquals } from "jsr:@std/assert"

const
symbol_h = Symbol("symbol h"),
symbol_i = Symbol("symbol i"),
symbol_j = Symbol("symbol j")

class A {
a = { v: 1 }
b = { v: 2 }
c: { v: number }
d() { return { v: 4 } }
e() { return { v: 5 } }
f = () => { return { v: 6 } }
g: () => ({ v: number })
[symbol_h]() { return { v: 8 } }
[symbol_i] = () => { return { v: 9 } }

constructor() {
this.c = { v: 3 }
this.g = () => { return { v: 7 } }
}
}

class B extends A {
override a = { v: 11 }
override e() { return { v: 15 } }
//@ts-ignore: typescript does not permit defining a method over the name of an existing property
override g() { return { v: 17 } }
[symbol_j] = () => { return { v: 20 } }

constructor() { super() }
}

const
a = new A(),
b = new B()

assertEquals(b.a, { v: 11 })
assertEquals(b.b, { v: 2 })
assertEquals(b.c, { v: 3 })
assertEquals(b.d(), { v: 4 })
assertEquals(b.e(), { v: 15 })
assertEquals(b.f(), { v: 6 })
assertEquals(b.g(), { v: 7 }) // notice that the overridden method is not called, and the property is called instead
assertEquals(Object.getPrototypeOf(b).g(), { v: 17 })
assertEquals(b[symbol_h](), { v: 8 })
assertEquals(b[symbol_i](), { v: 9 })
assertEquals(b[symbol_j](), { v: 20 })

assertEquals(
new Set(getOwnPropertyKeys(a)),
new Set(["a", "b", "c", "f", "g", symbol_i]),
)
assertEquals(
new Set(getOwnPropertyKeys(Object.getPrototypeOf(a))),
new Set(["constructor", "d", "e", symbol_h]),
)
assertEquals(
new Set(getOwnPropertyKeys(b)),
new Set(["a", "b", "c", "f", "g", symbol_i, symbol_j]),
)
assertEquals(
new Set(getOwnPropertyKeys(Object.getPrototypeOf(b))),
new Set(["constructor", "e", "g"]),
)