L
L
lotrop2016-09-02 13:32:41
JavaScript
lotrop, 2016-09-02 13:32:41

Javascript can't understand inheritance logic?

Here is one code

function inherit(proto) {
  function F() {}
  F.prototype = proto;
  var object = new F;
  return object;
}

Here is the second one:
function extend(Child, Parent) {
    var F = function() { }
    F.prototype = Parent.prototype
    Child.prototype = new F()
    Child.prototype.constructor = Child
    Child.superclass = Parent.prototype
}

Both of them implement the possibility of inheritance. But one point is not clear to me: Why in the first option:,
F.prototype = proto;
and in the second:
F.prototype = Parent.prototype
What is the difference in these approaches.
I still cannot understand why we write
F.prototype = Parent.prototype
, that is, we say, I want to put a parent on the F object, which will be the parent of Parent. Why for F to make the parent of the parent Parent. In a good way, Parent.prototype for F will be the grandfather. With my classical understanding of OOP, I can’t understand this, but as a given, until I understand, I won’t accept (

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nicholas, 2016-09-02
@healqq

prototype is not a parent. This is the class of the object.
In the second case, we make an empty constructor, then we say that this constructor makes objects of the Parent class.
After that, we create an object in which everything from the parent class is inherited in the prototype.
After that, we change the reference to the prototype constructor to match the object.

D
Dmitry Belyaev, 2016-09-02
@bingo347

The 1st option takes the parent's prototype in the parameter and returns the inherited prototype, what you will do with it is entirely up to you.
The 2nd option takes 2 constructors and does relatively correct inheritance

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question