Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

JS HasInstance

JSAPI method equivalent to the instanceof operator in JavaScript.

Syntax

JSBool JS_HasInstance(JSContext *cx, JSObject *obj, jsval *v, JSBool *bp);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JSObject * Constructor/prototype to test.
v jsval * The value to test.
bp JSBool * Out parameter. On success, *bp receives the result of the test.

Description

JS_HasInstance determines if a specified JS value, v, is an instance of JS object, obj. This function is equivalent to v instanceof obj test in JavaScript.  On success, JS_HasInstance stores the result of the instanceof test in *bp and returns JS_TRUE. On error or exception, it returns JS_FALSE, and the value left in *bp is undefined.

When providing a prototype as obj, the prototype's JSClass must have its hasInstance method set. Otherwise JS_HasInstance returns JS_FALSE.

MXR ID Search for JS_HasInstance

Document Tags and Contributors

 Contributors to this page: ethertank, TobiasFuenke, ashb
 Last updated by: ethertank,