📜 ⬆️ ⬇️

Inheritance without Prototype

In JavaScript, inheritance is done through the Prototype. Why not make inheritance not through it, it is not written anywhere. It is very difficult for a programmer who writes most in PHP or any other language where Prototype is the generating design pattern, and not a way of inheritance in JavaScript, to start writing classes, so almost no one actually writes them on their sites, but only in some sophisticated tasks or in special libraries.

Most sites do without classes. The maximum that can be expected is the use of some kind of JavaScript framework, where you can hope that it is present.

Tormented by idleness, as many before me, I tried to implement classes without a prototype. And actually, something happened, but what happened, I will tell you about it. Trying to find at least some advantages of this method, I saw that you can inherit in any desired order from any number of classes.
')
What is a class? Object type. And you can set the type of the object without a prototype. How to make links to parents? The first thing that came to mind is to copy all its methods and variables. Performance will surely suffer.

function Class(name, b, c) { window[name] = function(){ /*    */ for(var v in c) { if (!c.hasOwnProperty(v)) this[v] = c[v]; } /*   */ this.__bases = []; for(var i = 0; i < b.length; i++) { this.__bases.push([]); for (var v in b[i].__dict) { if (!c.hasOwnProperty(v) && v != 'construct') { this[v] = b[i].__dict[v]; } else { this.__bases[i][v] = function(obj, f) { if (typeof f == 'function') { return function() { f.apply(obj, arguments) }; } else { return f; } }(this, b[i].__dict[v]); } } } /*   */ if (c.hasOwnProperty('construct')) { c['construct'].apply(this, arguments); } else { for(var i = 0; i < b.length; i++) { if (b[i].__dict.hasOwnProperty('construct')) { b[i].__dict['construct'].apply(this, arguments); } } } }; window[name].__dict = c; } 


In MooTools, from the point of view of syntax, classes are best made, I got something similar

 Class('Person', [], { age: 0, name: '', construct: function(name, age) { this.name = name; this.age = age; }, who : function(){ return this.name + ' ' + this.age; } }); var v = new Person('Vova', 25); var w = new Person('Pavlik', 23); alert(w.who()); alert(v.who()); 


Inheritance goes from left to right and cannot be nested. If the constructor is not in the child class, all constructors of the parent are called in turn from left to right. Calling parent methods can be implemented through the __bases variable:

 Class('Student', [Person], { subjects: [], construct: function(name, age, subjects) { this.__bases[0].construct(name, age); this.subjects = subjects; }, who : function(){ return 'Student '+ this.name + ' ' + this.age + ' ' + this.subjects.join(', '); } }); var v = new Student('Vova', 25, ['Maths', 'History']); alert(v.who()); 


At this point I finished my research. I want to hear opinions on this method to create objects. What are your disadvantages of this approach?

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


All Articles