P
P
platedz2013-01-10 01:08:44
JavaScript
platedz, 2013-01-10 01:08:44

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

8 answer(s)
I
Ilya Lesnykh, 2013-01-10
@platedz

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:
{};.prototypewhich 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

P
Power, 2013-01-10
@Power

{}.prototype, in the general case, it undefinedalso makes little sense. (not to be confused with Object.prototype)
Show where you saw this.

K
Konstantin Kitmanov, 2013-01-10
@k12th

This is a big topic. One of the best explanations , IMHO.

P
platedz, 2013-01-10
@platedz

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
Ilya Sevostyanov, 2013-01-11
@RUVATA

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.

A great way to practice is to recharge with a fresh portion of knowledge obtained from books and examples, sit down on any forum, the same javascript.ru, and follow the questions, try to find an answer to them, and turning it into a working code with all possible theoretical explanations, since people go there just with a “rake” and “non-obviousness”, the “skill” is pumped over very well, it is not uncommon that they still manage to feel something real there, with a completely understandable “use case”.
Well, in fact, no one has canceled such a method of practice as “bicycle building”, i.e. give up some ready-made solution and try to implement the functionality you need from it yourself, and then take a look at the source code of the ready-made solution and then figure out why it’s not right for you :) (very delivering) for example - try to abandon jQuery to do something small, but run through all browsers - you are provided with a tour of the inner workings of browsers, and with it a lot of JavaScript code. Feel insecure in understanding the asynchronous programming style - try playing around with Node.JS - grab asynchrony in its purest manifestation.
PS: It’s also just for practice - it’s very useful to write tests, again preferably without involving third-party solutions, trust me, the test code is very rarely smaller than the application code :), I usually do this when there are no current tasks related to JavaScript, but “ leg" (in the sense of my JavaScript) was painfully numb. I take something old and let's cover it with tests ... (And I'll write a lot of code, and you see, I'll find some problems, I'll rewrite something). Here.

_
_ _, 2013-01-10
@AMar4enko

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.

P
platedz, 2013-01-10
@platedz

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.

P
platedz, 2013-01-10
@platedz

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 question

Ask a Question

731 491 924 answers to any question