-
Notifications
You must be signed in to change notification settings - Fork 390
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
Implement an object with multiple constructors #340
Comments
Hi. To be more standard compliant, you need something like this: vm := New()
intl := vm.NewObject()
dateTimeFormat := vm.ToValue(func(call ConstructorCall) *Object {
// initialise the instance (call.This) according to the arguments supplied to the constructor (call.Arguments)
return nil
})
dateTimeProto := dateTimeFormat.(*Object).Get("prototype").(*Object)
dateTimeProto.DefineDataProperty("format", vm.ToValue(func(call FunctionCall) Value {
instance, _ := call.This.(*Object)
// do the formatting using the instance parameters and the supplied arguments
_ = instance
result := vm.ToValue("passed")
return result
}), FLAG_TRUE, FLAG_TRUE, FLAG_FALSE)
intl.DefineDataProperty("DateTimeFormat", dateTimeFormat, FLAG_TRUE, FLAG_TRUE, FLAG_FALSE)
vm.GlobalObject().DefineDataProperty("Intl", intl, FLAG_TRUE, FLAG_TRUE, FLAG_FALSE)
res, err := vm.RunString(`
const formatter = new Intl.DateTimeFormat();
formatter.format();
`) |
You can also use a Go type as an instance, like this: type formatter struct {
}
vm := New()
intl := vm.NewObject()
dateTimeFormat := vm.ToValue(func(call ConstructorCall) *Object {
instance := &formatter{}
// initialise the instance according to the arguments supplied to the constructor (call.Arguments)
// instance.whatever = ....
instanceValue := vm.ToValue(instance).(*Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
})
dateTimeProto := dateTimeFormat.(*Object).Get("prototype").(*Object)
dateTimeProto.DefineDataProperty("format", vm.ToValue(func(call FunctionCall) Value {
instance, ok := call.This.Export().(*formatter)
if !ok {
panic(vm.NewTypeError("this should be a formatter"))
}
return vm.ToValue(formatter.format(....))
}), FLAG_TRUE, FLAG_TRUE, FLAG_FALSE) I'm going to add this to the documentation too. |
Awesome 🎉 This is very helpful. I have one more question about how I can get instance parameters inside the definition of "format" in the first solution.
|
You'd have to set them as properties of the instance (maybe Symbol properties to minimise the exposure). That's why the second solution is better. |
Sounds good to me 👍 Thank you very much.
|
No worries! |
Hi,
I'm implementing Intl for i18n support.
Intl
is actually a namespace that has multiple constructors. Can I use goja to implement it? I tried the following code. I expected to print out true, but got false. If I removenew
fromnew Intl.DateTimeFormat("haha")
, then it's working. I want to havenew
beforeIntl.DateTimeFormat("haha")
because that complies with specification.The text was updated successfully, but these errors were encountered: