L
L
lotrop2016-09-02 12:01:01
JavaScript
lotrop, 2016-09-02 12:01:01

OOP javascript why is this line needed?

Please explain why in this function

function extend(Child, Parent) {
  var F = function() { }
  F.prototype = Parent.prototype
  Child.prototype = new F()
  Child.prototype.constructor = Child
  Child.superclass = Parent.prototype
}

f.prototype = parent.prototype?
Why is F.prototype = Parent not done? I want my ancestor to be Parent, not his ancestor?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
napa3um, 2016-09-02
@lotrop

https://m.habrahabr.ru/post/140810/

G
GreatRash, 2016-09-02
@GreatRash

Because in Parent there can be static methods which in child nafig didn't rest.

var Parent = (function() {
  // конструктор
  function Parent() {}

  // статический метод
  Parent.staticMethod = function() {
    alert(1);
  };

  // обычный метод
  Parent.prototype.publicMethod = function() {
    alert(2);
  };

  return Parent;
}());

// в Child.prototype должен попасть только publicMethod

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question