javascript-questions icon indicating copy to clipboard operation
javascript-questions copied to clipboard

Answer to quesion 14 is wrong (well, right multi-choice answer, but wrong explanation)

Open jonrandy opened this issue 1 year ago • 3 comments

You say:

All objects have prototypes, except for the base object.

This is incorrect. It's trivial to create an object without a prototype...

const objectWithNoPrototype = Object.create(null)

jonrandy avatar Mar 17 '24 07:03 jonrandy

Interesting question of semantics - I'd argue that it's wrong, because your example does have a prototype, and that prototype is the null value (after all, typeof null is "object").

LeszekSwirski avatar May 17 '24 01:05 LeszekSwirski

But then, wouldn't the original also be wrong because the prototype of the base object is null too? 'All objects have prototypes, except for the base object.' suggests it doesn't have one (or that having null as a prototype is equivalent to not having one).

From MDN - "The prototype of Object.prototype is null, so it's at the end of the prototype chain"

So, based on the phrasing of the initial statement it is suggested that objects must have a prototype, and cannot be like the base object which 'does not have one'... which is not correct.

But yeah, semantics play into this a lot.

jonrandy avatar May 17 '24 02:05 jonrandy

Yeah I don't consider the original explanation to be correct either. It kind of depends on what you consider to be an "object" - is null an object, since it has typeof "object"? Or are we saying "object" is any non-primitive? If it's the former, then "false" is correct, with "null" being the counter example. Otherwise, "true" would be correct, because every non-primitive has a prototype (but that prototype can be a primitive, so there doesn't have to be an infinite depth prototype chain - but there can be!).

LeszekSwirski avatar May 17 '24 09:05 LeszekSwirski