Question presented to candidate: "If a real Error object is created inside an iframe (or a Node vm context) and passed to your main page's code, does instanceof Error correctly identify it as an Error there? What would you use instead?"
What a strong answer should cover:
- 📌 Interview term: a realm — a separate global execution environment with its OWN set of built-in constructors (
Object,Array,Error, etc.) — an iframe, a Web Worker, or (in Node) avmcontext each create a genuinely SEPARATE realm from the main one. - 📌 Interview term: the real, direct answer to the prompt — verified directly, using Node's own real
vmmodule to construct a genuinely separate realm: anErrorobject created in that OTHER realm genuinely failsinstanceof Errorwhen checked against THIS realm's ownErrorconstructor —instanceofcompares against a SPECIFIC constructor's prototype, and the other realm'sError.prototypeis a genuinely different object from this realm's. - 📌 Interview term:
Error.isError(value)— the real, purpose-built fix: verified directly, it correctly identified the SAME cross-realm Error object astrue, despiteinstanceoffailing on the identical value — it checks a real, internal engine-level tag rather than comparing against one specific realm's prototype. - 📌 Interview term:
Error.isErrorvs. plain objects/strings — verified directly: it correctly returnsfalsefor a plain object or a string that merely LOOKS error-like, confirming it is genuinely checking for a real Error internally, not just duck-typing based on amessageproperty. - A precise answer names that this cross-realm problem is not unique to
Error— the identical real issue affectsinstanceof Array/instanceof Dateacross realms too, which is exactly whyArray.isArray()(this bank's own array-methods coverage references it) has existed as the correct, realm-safe check for arrays for far longer;Error.isErroris genuinely the same real fix, just for errors specifically, and a much newer addition.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering WHY
instanceoffails (cross-realm prototype mismatch, not a general instanceof bug) is the strong signal.
Code / implementation expected: Yes — a real, genuine cross-realm test (using Node's own vm module to actually construct a separate realm) is the clearest, most convincing demonstration, rather than merely asserting the problem exists.