-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listing class properties #584
Comments
In general, you can just use built-in functions ( var getOwnPropertyNames func(o *Object) []string
err := vm.ExportTo(vm.Get("Object").ToObject(vm).Get("getOwnPropertyNames"), &getOwnPropertyNames)
if err != nil {
t.Fatal(err)
}
// ...
t.Log(getOwnPropertyNames(myClassInstance.Prototype())) // prints [constructor DoThing] or, to make it perform better, by using raw calls: getOwnPropertyNames, ok := AssertFunction(vm.Get("Object").ToObject(vm).Get("getOwnPropertyNames"))
if !ok {
t.Fatal("Object.getOwnPropertyNames is not a function")
}
// ...
array, err := getOwnPropertyNames(nil, myClassInstance.Prototype())
if err != nil {
t.Fatal(err)
}
t.Log(array.ToObject(vm).Get("1")) // prints DoThing However, in this particular case I think it makes sense to add it, I'll do it shortly. |
Great, it would be ideal to be able to inspect class methods without having to create an instance of the object. |
Class methods are non-enumerable properties of the class prototype, so even if you have an instance, you need to use Object.getOwnPropertyNames( |
Ah ok, I had to do |
Yes. |
It does not seem there is a goja equivalent of
get_own_property_names
in v8, which allows you to do things like inspect what methods exist on a class. The only thing I've found that can be pulled is what variables exist on an instance:But I am failing to find how I would, from a constructor, lis tthe
DoThing
method without prior knowledge of it. For reference the respective (rust) v8 looks like:Prints:
The text was updated successfully, but these errors were encountered: