Question presented to candidate: "If a constructor function explicitly does return { y: 2 } at the end, what does new SomeConstructor() actually give you back — the newly created instance, or that returned object?"
What a strong answer should cover:
- 📌 Interview term: what
newactually does, step by step — creates a brand-new, empty object; links that object's prototype to the constructor function's own.prototype; calls the constructor function withthisbound to that new object; and, by default, returns that object — verified directly, each step confirmed with a real constructor and prototype method. - 📌 Interview term: the real, direct answer to the prompt — verified directly: if the constructor explicitly returns an OBJECT,
newgenuinely returns THAT object instead of the newly-createdthis— the real object literal{ y: 2 }genuinely wins, and the properties set onthis(likex) are genuinely discarded/inaccessible from the result. - 📌 Interview term: an explicit PRIMITIVE return is ignored — verified directly, in real, sharp contrast: if the constructor instead returns a primitive (a number, string, boolean),
newgenuinely ignores it entirely, still returning the realthisobject as normal. - 📌 Interview term:
new.target— verified directly: a function can detect whether it was genuinely invoked withnewat all vianew.target, which is genuinelyundefinedfor a plain call and genuinely set for anewcall. - A precise answer names that calling a regular function WITHOUT
newgenuinely does not create any instance at all (thisfollows the normal call-site rules covered in this bank's own call/apply/bind questions), and that calling an ARROW function WITHnewgenuinely throws a realTypeError, since arrow functions are deliberately non-constructible.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering the prompt's own explicit-object-return scenario is the strong signal, since it is the single most commonly-missed
newdetail.
Code / implementation expected: Yes — the direct object-return-vs-primitive-return contrast is the clearest, most convincing demonstration of new's real, precise behavior.