This question has already been answered:

Please explain why the function call occurs?

Foo = function() { document.write("Hello"); } var Bar = function() { Foo.call(this); } Bar.prototype = new Foo(); 

Do I understand this code correctly?

I reason like this:

1) The normal foo() function simply prints "Hello"

2) In the body of the Bar() function, Foo() is called in the context of the Bar.prototype object? I make a similar conclusion, since this in the case of calling the function Foo() with new should point to the object that is the left operand of the assignment operation (clumsy, but this is from observations and hardware)!

3) Well, the last thing that is not clear to me. Bar.prototype is the same object reference that will be the prototype for objects created using the Bar() function? That is, we assign to the Bar.prototype object a new object constructed with the help of new , in whose body the only document.write() instruction, in general, for the first time, I see that there are Bar.prototype constructions other than properties and "methods"?

Please answer my question, do I reason correctly, although judging by what I don’t understand, see a space somewhere! In general, I am waiting for answers from experienced programmers in this question, thank you for your attention! ps: please do not ask me questions, like "well, what do you think you yourself (privetdev incidentally)." If I understood everything, I would not take your time, dear! Thank!

Reported as a duplicate at Grundy. javascript May 4 at 8:45 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    I can give you advice. Find a normal example of the implementation of prototype inheritance and then questions will disappear. Because an example that does not make anything intelligible makes it difficult to give an intelligible answer. - Stepan Kasyanenko

1 answer 1

The example you really strange, but let's see

 Foo = function() { document.write("Hello"); } Foo(); // Просто вызов функции new Foo(); // Вернет нам экземпляр Foo, сейчас это пустой объект 

Yes, Foo now just a function, but it can still be called with new , it just returns an instance of Foo , in this case it is just an empty object

Go ahead:

 var Bar = function() { Foo.call(this); } 

Again, you rightly said that this is just a call to the Foo function in the context of this , but there is a nuance, this context is not always equal to the Bar instance, it all depends on how Bar is called

 const Foo = function () { console.log(this); console.info('Invoke!'); } const Bar = function() { Foo.call(this); } Bar(); // контекст глобальный и может быть как `window`, так и что-то другое new Bar(); // контекст экземпляр Bar 

Last line:

 Bar.prototype = new Foo(); 

Overwritten the prototype, now it is again an empty object, if there were any methods / sv-va, they would be available in instances of Bar

Conclusion :
The example is really strange, it is better to look for more practical examples or switch to classes that are available in js . These are all the same prototypes, but it looks more and more semantic, or https://learn.javascript.ru/es-class

  • First, thank you for your efforts! But the sad thing is that I did not understand, due to which the function Foo() called, why did the "Hello" !? What does it mean to create an object of an object to which execute commands like doc.write () are passed? After all, if you simply create an object using a literal description, and add doc.write () there nothing appears in the workspace! In short, I have a mess in my head, I do not understand this syntax, although I seem to understand everything that you wrote - Muranx
  • now for the sake of interest in codepen I did function foo(){ document.write("Gizza is an object") } and then new foo() I don’t know how it turned out that this phrase appeared in the document, because I just created empty object with new foo() command based on foo() function - Muranx
  • one
    @Muranx see, the function is executed , and then the object is returned, that is, the code inside the function is executed on any call, no matter with new or without it, what is returned after the call is ThisMan
  • hmm, ok, already something! Well, now the point "G" , see. . . After all, Bar.prototype is the same prototype object for all instances that will be created using Bar() right? And what are we doing? Assign a new object to this object with the help of the foo() constructor (I feel it is not for nothing that you told me about a not quite smart code)? And about this , it turns out in the last operation this will point to the object that is created as a result of bar.prototype=foo() ? This is according to Kyle Simpson (You dont know JS) book series - Muranx
  • new Bar() will call Foo in the context of an instance of Bar , which in the prototype will have an instance of Foo , but at the output we will still receive an instance of Bar - ThisMan