Answer the question
In order to leave comments, you need to log in
What is {}.prototype?
I often see an entry like… = {}.prototype =… But I didn’t find what it is and how it can be used. Tell me please?
Answer the question
In order to leave comments, you need to log in
Wrong. First, torbasow is wrong, {}.prototype is not a SyntaxError. Such an error is caused due to the fact that the interpreter puts a semicolon after the finished construction {} and the following is obtained:
{};.prototype
which produces such a result.
If we write it like this:
var o = {}; o.prototype;
then, as already noted, this will return undefined to us .
Further, regarding the assignment,
user = {}.prototype = root
apparently you are not aware that the assignment operation has priority from right to left, that is, the right operand is executed first, then the one to the left, and so on, until it comes to the leftmost one. Example:
var3 = 10; var var1 = var2 = (var3 + 1); console.log('1: '+var1); console.log('2: '+var2);
output
1:11
2:11
{}.prototype
, in the general case, it undefined
also makes little sense. (not to be confused with Object.prototype
)
Show where you saw this.
To be honest, I thought that {}.prototype and Object.prototype are the same, as are Array.prototype and [].prototype. I would appreciate an explanation.
I would like to somehow consolidate all this in practice so that it is not forgotten, but gaps in knowledge do not allow us to realize our plans.
I could be wrong, but apparently this is a global prototype definition for Object.
Those. subsequently, when creating any object, methods defined through prototype will be available to it.
You can add a method to an object at any time. And through the prototype, you add it to all instances of Object at once.
Thank you very much for the replies.
Object -> Object.prototype
Function -> Function.prototype ->
Object.prototype Object.prototype is sort of the parent of all objects (including functions), whether predefined or not. First, properties and methods are searched for the object itself, and then, in order, for all its prototypes, including until they reach Object.prototype.
Do I get it right?
I even assume that constructors are created precisely due to the fact that Function.prototype contains all the methods and properties necessary for this. And if I'm right, it would not be superfluous to know which ones.
Also heredescribed that you can override the Object.prototype property. And as far as I understand, the same can be done for the properties of other predefined objects. It is not clear how all this happens, i.e. how do I assign, say, a new property or method, and most importantly, check and see (make sure) that everything worked out.
And also it is not clear that if I need to establish not a reference to an object, but simply copy the object, and then supplement it with new properties, i.e. if i want admin to get properties and methods of root. But at the same time, further changing properties and methods or adding them did not affect root. As well as changing the properties of root, did not affect the properties of the admin object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question