📜 ⬆️ ⬇️

Legacy or Inheritance: a bit offtopic about .prototype, pun intended for a neophyte

I will try to be incredibly brief.

Written for those who are just starting!

There are many articles about inheritance in JS:

')
I consider it important to add that there is a terminological difference at the level of native speakers, i.e. - English. And it is crucial when you work with prototypes in JS.

In my google on the query " what is the difference between inheritance and legacy " the first link leads here: link .

Of course, that the articles there are not one.

There is a line like this: “ Also, as it’s been noted,” while it’s legacy, it’s not .

The brief essence, which does not reflect all the depth that I have understood myself, is that:


Therefore, I believe that in ordinary OOP programming languages ​​such as Java, C #, C ++, etc. the term inheritance is used .

And here in JavaScript we use almost the same thing, but a bit wider: legacy is a legacy .

And since in Russian there is almost no difference between these concepts, then there is a mass of FuckUps with an understanding of why this is necessary.

And, including because of the Legacy code, we are not going anywhere.

UPD 2 : Extract from legacy code >> Modern interpretations .

... source code inherited from an older version of the software ...

UPD 3 : Under the cut there is a speech that Inheritance inevitably entails Legacy. More precisely, on the contrary, according to the terminology of speakers of the Legacy language, this is more than Inheritance.

UPD 1: The comments suggest that it may be partly about the Sapir-Whorf Hypothesis . Thank you k12th . Also, thanks to lair for constructive criticism.


Here is the code from Node.JS REPL> console.log (util.inherits.toString ())
function (ctor, superCtor) { ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, enumerable: false, writable: true, configurable: true } }); } 

(if anyone is interested, looking through this tool , use caution, since I am the author)


Here is the usual code that is commonly used:
 function inherit(Child, Parent) { var BlankClass = function () {}; BlankClass.prototype = Parent.prototype; Child.prototype = new BlankClass(); Child.prototype.constructor = Child; Child.prototype.superclass = Parent; }; 


The same code, but from the site javascript.ru,
The same code, but from the site javascript.ru
 function extend(Child, Parent) { var F = function() { }; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.superclass = Parent.prototype; }; 


javascript.ru/tutorial/object/inheritance

Note the term Inheritance is used!

Yes, and here it is probably important to clarify the position, because I am for this approach, for both approaches. But, like all of us, I am against the legacy code in a broad sense. But this is not going anywhere.

As a result, we usually do this:

 var SomeChild = function () {}; var SomeParent = function () {}; inherit (SomeChild, SomeParent); SomeChild.prototype.overload_something = 'something_we_need_to_FuckUp'; 


As a result, in fact, “inheritance with method overloading” is realized.

There are no problems here, it seems, because everyone is used to it. This is the "standard" implementation, and from it "nowhere." It is based on the Inheritance paradigm.

But what does the legacy have to do with it?

I believe that despite the fact that, whatever one may say, in this case the look will always be from the side of the Parent . In order to understand the redefined code, you need to look at what methods the parent had, and his parent , and if there were more branches there, and so on, and so forth . It seems that it always has been, and everyone should assume that it is so. This is architecture and design.

This is a mantra.

And it seems that everything is written about it everywhere, and this is explained to all beginners.

But this is not enough, because:

>> Legacy, will be of broader or deeper scope. When inheritance refers to one generation of gifts to another, a legacy would refer to three or four generations. >>

That is, here, for anybody there is no news that there is a prototype chain .

But how to use it, and why everything is “so”, it may not be entirely clear even to a novice.

So please, keep in mind that there is a difference between legacy and inheritance. And that for native speakers it is even more obvious than for us, not native speakers. Just for some reason, it often seems to beginners that it is almost nonexistent, so-so are synonymous.

And finally, the most important thing, in my opinion, is what is not added in UPD 3:
The term Legacy includes the term Inheritance, both in their and ours. The difference became clear to me not so long ago. And, the next time I’m going to write .prototype, I’ll remember that this approach inevitably comes from having Legacy. Therefore, I will think 7 times if I can do without it. Maybe other language tools will be enough for me to write these few lines of text. And, if I have to use it all the same, then “throwing” the code of the next Child, I will remember that for the Parent - this is Legacy.

Source: https://habr.com/ru/post/213441/


All Articles