Re: Create a new native object with a constructor
Braden McDaniel <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.jseng |
|---|---|
| Message-ID | <[email protected]> |
On Sun, 2016-06-19 at 21:19 -0400, Boris Zbarsky wrote:
> On 6/19/16 9:05 PM, Braden McDaniel wrote:
> > What's the modern way to create a new native object that has a
> > constructor?
>
> Has a constructor in what sense? Do you mean that there is a
> JSNative
> constructor that you want to get called that creates the object?
This. That is, the JSClass::constructor member for the JSClass that
defines the native object implementation is not null.
> > The preferred alternative is to save a copy of the constructor
> > function for the class, then to call it using JS_New.
> >
> > But that doesn't look like the right answer for a natively
> > implemented
> > object (since JS_NewObject still does look like part of the answer
> > for
> > natively implemented objects).
>
> This comes back to my question above. If the constructor JSNative
> creates the object, then just calling it via JS_New will do the
> right
> thing; it'll do the JS_NewObject itself.
Ah, got it. Thanks.
On a related note, the JS_InitClass documentation includes this:
If you provide a constructor for the class, then you should also
pass an object to parent_proto. JS_InitClass uses parent_proto to
build a prototype object for the class. The prototype object
inherits properties and methods from the parent_proto object you
provide.
I gather, then, that passing null for parent_proto is insufficient even
if the parent is Object? Is there some convenient way to get the proto
for Object in order to pass it here?
--
Braden McDaniel <[email protected]>
_______________________________________________
dev-tech-js-engine mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine