mirror of
https://github.com/robertkrimen/otto
synced 2025-10-12 20:27:30 +08:00
Add Object.getOwnPropertyNames (sdgoij)
This commit is contained in:
parent
716c307c8d
commit
61a011e711
|
@ -223,7 +223,7 @@ func builtinObject_freeze(call FunctionCall) Value {
|
|||
}
|
||||
|
||||
func builtinObject_keys(call FunctionCall) Value {
|
||||
if object, keys := call.Argument(0)._object(), []Value{}; nil != object {
|
||||
if object, keys := call.Argument(0)._object(), []Value(nil); nil != object {
|
||||
object.enumerate(false, func(name string) {
|
||||
keys = append(keys, toValue(name))
|
||||
})
|
||||
|
@ -231,3 +231,15 @@ func builtinObject_keys(call FunctionCall) Value {
|
|||
}
|
||||
panic(newTypeError())
|
||||
}
|
||||
|
||||
func builtinObject_getOwnPropertyNames(call FunctionCall) Value {
|
||||
if object, propertyNames := call.Argument(0)._object(), []Value(nil); nil != object {
|
||||
object.enumerate(true, func(name string) {
|
||||
if object.hasOwnProperty(name) {
|
||||
propertyNames = append(propertyNames, toValue(name))
|
||||
}
|
||||
})
|
||||
return toValue(call.runtime.newArrayOf(propertyNames))
|
||||
}
|
||||
panic(newTypeError())
|
||||
}
|
||||
|
|
|
@ -184,6 +184,7 @@ func newContext() *_runtime {
|
|||
"isFrozen", -1, builtinObject_isFrozen,
|
||||
"freeze", -1, builtinObject_freeze,
|
||||
"keys", -1, builtinObject_keys,
|
||||
"getOwnPropertyNames", -1, builtinObject_getOwnPropertyNames,
|
||||
)
|
||||
|
||||
self.Global.Function = self.newGlobalFunction(
|
||||
|
|
|
@ -271,3 +271,25 @@ func TestObject_keys(t *testing.T) {
|
|||
})(undefined, undefined, undefined, undefined);
|
||||
`, "0,1,2,3")
|
||||
}
|
||||
|
||||
func TestObject_getOwnPropertyNames(t *testing.T) {
|
||||
Terst(t)
|
||||
|
||||
test := runTest()
|
||||
|
||||
test(`Object.getOwnPropertyNames({ abc:undefined, def:undefined })`, "abc,def")
|
||||
|
||||
test(`
|
||||
var ghi = Object.create(
|
||||
{
|
||||
abc: undefined,
|
||||
def: undefined
|
||||
},
|
||||
{
|
||||
ghi: { value: undefined, enumerable: true },
|
||||
jkl: { value: undefined, enumerable: false }
|
||||
}
|
||||
);
|
||||
Object.getOwnPropertyNames(ghi)
|
||||
`, "ghi,jkl")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user