javascript-tutorial / en.javascript.info

Modern JavaScript Tutorial
https://javascript.info
Other
23.04k stars 3.82k forks source link

Not quite complete statement in summary #3663

Open Mtlekvin opened 5 months ago

Mtlekvin commented 5 months ago

ffdsfsd https://javascript.info/function-prototype - on this page in the summary we have this statement:

The value of F.prototype should be either an object or null: other values won’t work.

But here we have this statement:

If the prototype of a function is reassigned with something other than an Object, when the function is called with new, the returned object's prototype would be Object.prototype instead. (In other words, new ignores the prototype property and constructs a plain object.)

So I think it's worth mentioning that F.prototype can be equal to null only when Object.prototype, but when you set prototype for F.prototype it's not possible.

prakhar-pal commented 5 months ago

A newly created object will have Object as it's prototype, however Object.prototype would always is null.

Basically the prototype chain has to stop somewhere and it's indicated by null.

prakhar-pal commented 5 months ago

My earlier explanation is incorrect, I could have said this better. To quote from MDN docs

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes

Object.prototype will be equal to any primitive value as well, not just null. For example Object.prototype can be equal to 5.

This is false. Maybe you can provide some code example/docs for reference.

The following is what I get in NodeJS repl.

> Object.prototype
[Object: null prototype] {}

how does Object.prototype affect something when its instances do not get a prototype equal to null or any other primitive?

To answer this, I'm again quoting MDN

When you try to access a property of an object: if the property can't be found in the object itself, the prototype is searched for the property. If the property still can't be found, then the prototype's prototype is searched, and so on until either the property is found, or the end of the chain is reached, in which case undefined is returned.

Mtlekvin commented 5 months ago

This is false. Maybe you can provide some code example/docs for reference.

Yes, I mistook Object for Function.

I understood what you meant to say and corrected the title of the issue to a different one.