javascript - Why can a constructor only return an object? -
if there constructor like
function a() {} then
(new a) instanceof === true but on other hand,
function a() { return {} } results in
(new a) instanceof === false so thinking that
function a() { return 123 } would result in same thing. however, when returning number,
(new a) instanceof === true how possible? why can't make constructor return else object?
(i know making constructor returning number rather useless understand 'why' of behaviour)
according spec: if calling constructor returns object, object result of new-expression. if constructor doesn't return object (but undefined or other primitive value), result newly created object.
if primitives allowed, constructors have explicitly return (typically "this"), otherwise result undefined (because result of function without return undefined). needless hassle.
additionally, makes sense new can relied on return object.
Comments
Post a Comment