JS IsConstructing
From MDC
Determines whether the function currently executing in a given context was called as a constructor.
[edit] Syntax
JSBool JS_IsConstructing(JSContext *cx);
| Name | Type | Description |
|---|---|---|
cx |
JSContext * |
The context to examine. |
[edit] Description
JS_IsConstructing determines whether or not the function currently executing in cx was called as a constructor (either from JavaScript, using the new keyword, or from C/C++ using a JSAPI function such as JS_ConstructObject). If it was, JS_IsConstructing returns JS_TRUE. Otherwise it returns JS_FALSE.
A JSNative can use this to determine whether it was called as a constructor.
If called from a JSFastNative, JS_IsConstructing will likely return the wrong answer. It will check whether the JSFastNative's caller, not the JSFastNative itself, was called as a constructor. (This happens because JS_IsConstructing works by examining the top stack frame on the JavaScript stack, and JSFastNatives do not have stack frames.)
If your constructor function does not verify if it is invoked as a function/method directly instead of as a constructor using new, it is possible for the application to crash because the this object is not of the expected type. The constructor may want to throw an exception when called as a method, for instance, instead of proceeding as usual, or to behave in another manner.