What is the best way to make inheritance in JavaScript?
Closed due to the fact that it is necessary to reformulate the question so that it is possible to give an objectively correct answer by the participants of Wiktor Stribiżew , aleksandr barakin , 0xdb , Misha Saidov , Air 15 Jan at 9:10
The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .
3 answers
It is best to use the method with fake constructor:
function inherit (object, parent) { function F(){}; // Подставной конструктор F.prototype = parent.prototype; // Подсовываем прототип реального конструктора object.prototype = new F(); // Теперь реальный конструктор не будет выполнен return object; // Можно и не возвращать };
__extends
function:
function(child, parent) { for (var key in parent) { if (parent.hasOwnProperty(key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
There are many ways out, the best, IMHO - the use of the prototype. See here for more details.
In javascript, base inheritance is not based on classes. That is, there is no such thing that classes inherit from each other, and the object of the descendant class receives common properties.
Instead, objects inherit from objects without any classes. Inheritance on classes can be built (emulated), based on the basic javascript inheritance.
- Well, there is a prototype used differently. This is the normal version of the function SomeClass (_Arg1, _Arg2) {Parent1.call (this, Arg1); Parent2.call (this, Arg2); // further, own methods and properties of SomeClass} I personally like to do it through closure - Zow
- "This is the normal version." Not a single JS environment known to me, from tz. performance does not agree with your statement. (and, sometimes, talking about very serious numbers) - Zowie
- "This is the normal version" that was the question. Well, I do not really like it. I do through a closure or a factory of objects, as it is called - Zow
prototype is a property of a constructor function that points to the object on the basis of which it makes new objects. Not in the sense that it contains the ancestor of the constructor function, but in the fact that it contains the ancestor for all the objects that this function creates.
The constructor is a property of the object that indicates the function by which it is made. TwoDShape.prototype = new Shape ();
sets as a base object the object-result of the Shape function, including the constructor property, which indicates that the object was made by the Shape function.
new TwoDShape () will create an object, the constructor property of which will indicate that it is created by the Shape function. So that it is correctly stated that it is made by the TwoDShape function, they make
TwoDShape.prototype.constructor = TwoDShape;
In general, the prototype inheritance mechanism of the implementation itself is through the hidden proto property; it contains a reference to the prototype object and this chain of these javascript objects passes when searching for properties and methods. And the constructor simply indicates the function that created the object, and nothing more. If it is changed, it will indicate the correct, if not, then the function that created the topmost object in the prototype chain.