Subj : Re: JSClass construct member versus JS_InitClass constructor parameter To : Thomas Sondergaard From : Brendan Eich Date : Thu Jul 28 2005 01:42 pm Thomas Sondergaard wrote: > I've noticed that JSClass has a 'construct' member of type JSNative. > JS_InitClass has a parameter 'constructor'. I can't seem to figure out > when the 'construct' function is invoked. the 'constructor' parameter > passed to JS_InitClass seems to be called when an instance of the class > is created. The JSClass hook, if non-null, is used when you invoke the object of that class as a constructor, not when you construct instances of that object's class by calling a predefined constructor function. Case 1, calling a predefined constructor function: In 'o = new C', the new object assigned to o was indeed passed to the constructor JSNative, provided C is the name of the JSClass that you initialized via JS_InitClass, with that native constructor. In other words, part of what JS_InitClass is to define a native function with that JSNative entry point, having the name of the class, as a property of the global object (the obj 2nd parameter to JS_InitClass). Case 2, using an arbitrary object as a constructor: In the case 1 example, C is itself an object, and if its class has a non-null construct hook, that JSNative will be called. This hook is not the same as the native entry point of the function object identified by the property name "C" in any object. If there were such a property C denoting a native function object, we would be dealing with case 1. Instead, this case is about an object that may have a role to play as a constructor, but that is not a function or necessarily even a callable object, at all. In general, if an object may be used as a constructor, it may be called as a function too. If there is some short-hand utility in the function call form doing anything other than constructing a new instance, then that may be done by convention (e.g. Date() vs. new Date). If there is no shorthand, then calling should default to constructing -- but need not (it's up to the object's implementation). Note that function objects do not have a non-null JSClass.construct hook, and are handled by more heavily inlined code in the interpreter. > Can someone tell me in plain english (or danish) what the intention is > with JSClass.construct? I hope the above helps. If not, ask questions. /be .