1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

add KeysByParent function for recursively getting object keys

This commit is contained in:
deoxxa 2016-04-27 16:08:07 +10:00
parent b283580aea
commit 6e7c8df250
2 changed files with 35 additions and 0 deletions

19
otto.go
View File

@ -655,6 +655,25 @@ func (self Object) Keys() []string {
return keys
}
// Get the keys (and those of the parents) for the object, in order of
// "closest" to "furthest"
func (self Object) KeysByParent() [][]string {
var a [][]string
for o := self.object; o != nil; o = o.prototype {
var l []string
o.enumerate(false, func(name string) bool {
l = append(l, name)
return true
})
a = append(a, l)
}
return a
}
// Class will return the class string of the object.
//
// The return value will (generally) be one of:

View File

@ -851,6 +851,22 @@ func TestAPI(t *testing.T) {
})
}
func TestObjectKeys(t *testing.T) {
tt(t, func() {
vm := New()
vm.Eval(`var x = Object.create(null); x.a = 1`)
vm.Eval(`var y = Object.create(x); y.b = 2`)
o1, _ := vm.Object("x")
is(o1.Keys(), []string{"a"})
is(o1.KeysByParent(), [][]string{{"a"}})
o2, _ := vm.Object("y")
is(o2.Keys(), []string{"b"})
is(o2.KeysByParent(), [][]string{{"b"}, {"a"}})
})
}
func TestUnicode(t *testing.T) {
tt(t, func() {
test, _ := test()