├── English ├── inheritance1.js ├── inheritance2.js ├── inheritance3.js ├── inheritance3.speedtest.js ├── inheritance4-1.speedtest.js ├── inheritance4-2.speedtest.js ├── inheritance4.js ├── inheritance5.js ├── inheritance5.speedtest.js ├── inheritance6.js ├── inheritance6.speedtest.js ├── inheritance7.js ├── inheritance7.speedtest.js ├── inheritance8.js ├── inheritance8.speedtest.js ├── inheritance9.js └── inheritance9.speedtest.js ├── README.md └── Russian ├── inheritance1.js ├── inheritance2.js ├── inheritance3.js ├── inheritance3.speedtest.js ├── inheritance4-1.speedtest.js ├── inheritance4-2.speedtest.js ├── inheritance4.js ├── inheritance5.js ├── inheritance5.speedtest.js ├── inheritance6.js ├── inheritance6.speedtest.js ├── inheritance7.js ├── inheritance7.speedtest.js ├── inheritance8.js ├── inheritance8.speedtest.js ├── inheritance9.js ├── inheritance9.speedtest.js └── tree.js /English/inheritance1.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Classes definition 4 | 5 | function ParentClass(par1, par2) { 6 | this.parentField1 = par1; 7 | this.parentField2 = par2; 8 | } 9 | 10 | function ChildClass(par1, par2) { 11 | ChildClass.super_.apply(this, arguments); 12 | this.childField1 = par1; 13 | this.childField2 = par2; 14 | } 15 | 16 | // Inheritance 17 | 18 | util.inherits(ChildClass, ParentClass); 19 | 20 | // Create child class instance and check results 21 | 22 | var obj = new ChildClass('Hello', 'World'); 23 | console.dir({ obj: obj }); 24 | 25 | /* Output: 26 | 27 | { obj: 28 | { parentField1: 'Hello', 29 | parentField2: 'World', 30 | childField1: 'Hello', 31 | childField2: 'World' } } 32 | 33 | */ 34 | -------------------------------------------------------------------------------- /English/inheritance2.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Parent class constructor 4 | function ParentClass(par1, par2) { 5 | this.parentField1 = par1; 6 | this.parentField2 = par2; 7 | } 8 | 9 | // Parent class method 10 | ParentClass.prototype.parentMethod = function(par) { 11 | console.log('parentMethod("' + par + '")'); 12 | }; 13 | 14 | // Parent class field 15 | ParentClass.prototype.parentField = 'Parent field value'; 16 | 17 | // Child class constructor 18 | function ChildClass(par1, par2) { 19 | ChildClass.super_.apply(this, arguments); 20 | this.childField1 = par1; 21 | this.childField2 = par2; 22 | } 23 | 24 | // Inheritance 25 | util.inherits(ChildClass, ParentClass); 26 | 27 | // Child class method 28 | ChildClass.prototype.childMethod = function(par) { 29 | console.log('childMethod("' + par + '")'); 30 | }; 31 | 32 | // Child class field 33 | ChildClass.prototype.childField = 'Child field value'; 34 | 35 | // Instantiation 36 | var parentClassInstance = new ParentClass('Marcus', 'Aurelius'); 37 | var childClassInstance = new ChildClass('Yuriy', 'Gagarin'); 38 | 39 | // Check results 40 | console.dir({ 41 | parentClassInstance: parentClassInstance, 42 | childClassInstance: childClassInstance 43 | }); 44 | 45 | console.dir({ 46 | objectFieldDefinedInParent: childClassInstance.parentField1, 47 | classFieldDefinedInParent: childClassInstance.parentField, 48 | objectFieldDefinedInChild: childClassInstance.childField1, 49 | classFieldDefinedInChild: childClassInstance.childField 50 | }); 51 | 52 | parentClassInstance.parentMethod('Cartesius'); 53 | childClassInstance.childMethod('von Leibniz'); 54 | 55 | /* Output: 56 | 57 | { parentClassInstance: 58 | { parentField1: 'Marcus', parentField2: 'Aurelius' }, 59 | childClassInstance: 60 | { parentField1: 'Yuriy', parentField2: 'Gagarin', 61 | childField1: 'Yuriy', childField2: 'Gagarin' } } 62 | { objectFieldDefinedInParent: 'Yuriy', 63 | classFieldDefinedInParent: 'Parent field value', 64 | objectFieldDefinedInChild: 'Yuriy', 65 | classFieldDefinedInChild: 'Child field value' } 66 | parentMethod("Cartesius") 67 | childMethod("von Leibniz") 68 | 69 | */ 70 | -------------------------------------------------------------------------------- /English/inheritance3.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Parent class constructor 4 | function ParentClass(par1, par2) { 5 | this.parentField1 = par1; 6 | this.parentField2 = par2; 7 | } 8 | 9 | // Parent class method 10 | ParentClass.prototype.methodName = function(par) { 11 | console.log('Parent method implementation: methodName("' + par + '")'); 12 | }; 13 | 14 | // Child class constructor 15 | function ChildClass(par1, par2) { 16 | ChildClass.super_.apply(this, arguments); 17 | this.childField1 = par1; 18 | this.childField2 = par2; 19 | } 20 | 21 | // Inheritance 22 | util.inherits(ChildClass, ParentClass); 23 | 24 | // Override method in child 25 | ChildClass.prototype.methodName = function(par) { 26 | // Invoke inherited method from parent class 27 | ChildClass.super_.prototype.methodName.apply(this, arguments); 28 | // Method code 29 | console.log('Child method implementation: methodName("' + par + '")'); 30 | }; 31 | 32 | // Child class instantiation 33 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 34 | 35 | // Check results 36 | childClassInstance.methodName('Tolstoy'); 37 | 38 | /* Output: 39 | 40 | Parent method implementation: methodName("Tolstoy") 41 | Child method implementation: methodName("Tolstoy") 42 | 43 | */ 44 | -------------------------------------------------------------------------------- /English/inheritance3.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function ParentClass(par1, par2) { 13 | this.parentField1 = par1; 14 | this.parentField2 = par2; 15 | } 16 | 17 | ParentClass.prototype.methodName = function(par) { 18 | this.parentField3 = par; 19 | }; 20 | 21 | function ChildClass(par1, par2) { 22 | ChildClass.super_.apply(this, arguments); 23 | this.childField1 = par1; 24 | this.childField2 = par2; 25 | } 26 | 27 | util.inherits(ChildClass, ParentClass); 28 | 29 | ChildClass.prototype.methodName = function(par) { 30 | ChildClass.super_.prototype.methodName.apply(this, arguments); 31 | this.childField3 = par; 32 | }; 33 | 34 | speedTest('Instantiation test', 10000000, function() { 35 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 36 | }); 37 | 38 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 39 | speedTest('Call method test', 10000000, function() { 40 | childClassInstance.methodName('Tolstoy'); 41 | }); 42 | -------------------------------------------------------------------------------- /English/inheritance4-1.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | Function.prototype.override = function(fn) { 13 | var superFunction = this; 14 | return function() { 15 | this.inherited = superFunction; 16 | return fn.apply(this, arguments); 17 | }; 18 | }; 19 | 20 | function ParentClass(par1, par2) { 21 | this.parentField1 = par1; 22 | this.parentField2 = par2; 23 | } 24 | 25 | ParentClass.prototype.methodName = function(par) { 26 | this.parentField3 = par; 27 | }; 28 | 29 | function ChildClass(par1, par2) { 30 | ChildClass.super_.apply(this, arguments); 31 | this.childField1 = par1; 32 | this.childField2 = par2; 33 | } 34 | 35 | util.inherits(ChildClass, ParentClass); 36 | 37 | ChildClass.prototype.methodName = ParentClass.prototype.methodName.override(function(par) { 38 | this.inherited(par); 39 | this.childField3 = par; 40 | }); 41 | 42 | speedTest('Instantiation test', 10000000, function() { 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | }); 45 | 46 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 47 | speedTest('Call method test', 10000000, function() { 48 | childClassInstance.methodName('Tolstoy'); 49 | }); 50 | -------------------------------------------------------------------------------- /English/inheritance4-2.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | Function.prototype.override = function(fn) { 13 | var superFunction = this; 14 | return function() { 15 | this.inherited = superFunction; 16 | return fn.apply(this, arguments); 17 | }; 18 | }; 19 | 20 | function ParentClass(par1, par2) { 21 | this.parentField1 = par1; 22 | this.parentField2 = par2; 23 | } 24 | 25 | ParentClass.prototype.methodName = function(par) { 26 | this.parentField3 = par; 27 | }; 28 | 29 | function ChildClass(par1, par2) { 30 | ChildClass.super_.apply(this, arguments); 31 | this.childField1 = par1; 32 | this.childField2 = par2; 33 | } 34 | 35 | util.inherits(ChildClass, ParentClass); 36 | 37 | ChildClass.prototype.methodName = ParentClass.prototype.methodName.override(function(par) { 38 | this.inherited.apply(this, arguments); 39 | this.childField3 = par; 40 | }); 41 | 42 | speedTest('Instantiation test', 10000000, function() { 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | }); 45 | 46 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 47 | speedTest('Call method test', 10000000, function() { 48 | childClassInstance.methodName('Tolstoy'); 49 | }); 50 | -------------------------------------------------------------------------------- /English/inheritance4.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Define override/inherited methods for built-in Function class 4 | Function.prototype.override = function(fn) { 5 | var superFunction = this; 6 | return function() { 7 | this.inherited = superFunction; 8 | return fn.apply(this, arguments); 9 | }; 10 | }; 11 | 12 | // Parent class constructor 13 | function ParentClass(par1, par2) { 14 | this.parentField1 = par1; 15 | this.parentField2 = par2; 16 | } 17 | 18 | // Parent class method 19 | ParentClass.prototype.methodName = function(par) { 20 | console.log('Parent method implementation: methodName("' + par + '")'); 21 | }; 22 | 23 | // Child class constructor 24 | function ChildClass(par1, par2) { 25 | ChildClass.super_.apply(this, arguments); 26 | this.childField1 = par1; 27 | this.childField2 = par2; 28 | } 29 | 30 | // Inheritance 31 | util.inherits(ChildClass, ParentClass); 32 | 33 | // Override method in child 34 | ChildClass.prototype.methodName = ParentClass.prototype.methodName.override(function(par) { 35 | // Invoke inherited method from parent class 36 | this.inherited(par); // or this.inherited.apply(this, arguments); 37 | // Method code 38 | console.log('Child method implementation: methodName("' + par + '")'); 39 | }); 40 | 41 | // Child class instantiation 42 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 43 | 44 | // Check results 45 | childClassInstance.methodName('Tolstoy'); 46 | 47 | /* Output: 48 | 49 | Parent method implementation: methodName("Tolstoy") 50 | Child method implementation: methodName("Tolstoy") 51 | 52 | */ 53 | -------------------------------------------------------------------------------- /English/inheritance5.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Define override/inherited methods for built-in Function class 4 | function override(fn) { 5 | var name = fn.name; 6 | return function() { 7 | this.inherited = this.constructor.super_.prototype[name]; 8 | return fn.apply(this, arguments); 9 | } 10 | } 11 | 12 | // Parent class constructor 13 | function ParentClass(par1, par2) { 14 | this.parentField1 = par1; 15 | this.parentField2 = par2; 16 | } 17 | 18 | // Parent class method 19 | ParentClass.prototype.methodName = function(par) { 20 | console.log('Parent method implementation: methodName("' + par + '")'); 21 | }; 22 | 23 | // Child class constructor 24 | function ChildClass(par1, par2) { 25 | ChildClass.super_.apply(this, arguments); 26 | this.childField1 = par1; 27 | this.childField2 = par2; 28 | } 29 | 30 | // Inheritance 31 | util.inherits(ChildClass, ParentClass); 32 | 33 | // Override method in child 34 | ChildClass.prototype.methodName = override(function methodName(par) { 35 | // Invoke inherited method from parent class 36 | this.inherited(par); // or this.inherited.apply(this, arguments); 37 | // Method code 38 | console.log('Child method implementation: methodName("' + par + '")'); 39 | }); 40 | 41 | // Child class instantiation 42 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 43 | 44 | // Check results 45 | childClassInstance.methodName('Tolstoy'); 46 | 47 | /* Output: 48 | 49 | Parent method implementation: methodName("Tolstoy") 50 | Child method implementation: methodName("Tolstoy") 51 | 52 | */ 53 | -------------------------------------------------------------------------------- /English/inheritance5.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function override(fn) { 13 | var name = fn.name; 14 | return function() { 15 | this.inherited = this.constructor.super_.prototype[name]; 16 | return fn.apply(this, arguments); 17 | } 18 | } 19 | 20 | function ParentClass(par1, par2) { 21 | this.parentField1 = par1; 22 | this.parentField2 = par2; 23 | } 24 | 25 | ParentClass.prototype.methodName = function(par) { 26 | this.parentField3 = par; 27 | }; 28 | 29 | function ChildClass(par1, par2) { 30 | ChildClass.super_.apply(this, arguments); 31 | this.childField1 = par1; 32 | this.childField2 = par2; 33 | } 34 | 35 | util.inherits(ChildClass, ParentClass); 36 | 37 | ChildClass.prototype.methodName = override(function methodName(par) { 38 | this.inherited(par); // или this.inherited.apply(this, arguments); 39 | this.childField3 = par; 40 | }); 41 | 42 | speedTest('Instantiation test', 10000000, function() { 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | }); 45 | 46 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 47 | speedTest('Call method test', 10000000, function() { 48 | childClassInstance.methodName('Tolstoy'); 49 | }); 50 | -------------------------------------------------------------------------------- /English/inheritance6.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Define override/inherited methods for built-in Function class 4 | 5 | function override(parent, fn) { 6 | fn.inherited = parent.prototype[fn.name]; 7 | return fn; 8 | } 9 | 10 | // Parent class constructor 11 | function ParentClass(par1, par2) { 12 | this.parentField1 = par1; 13 | this.parentField2 = par2; 14 | } 15 | 16 | // Parent class method 17 | ParentClass.prototype.methodName = function(par) { 18 | console.log('Parent method implementation: methodName("' + par + '")'); 19 | }; 20 | 21 | // Child class constructor 22 | function ChildClass(par1, par2) { 23 | ChildClass.super_.apply(this, arguments); 24 | this.childField1 = par1; 25 | this.childField2 = par2; 26 | } 27 | 28 | // Inheritance 29 | util.inherits(ChildClass, ParentClass); 30 | 31 | // Override method in child 32 | ChildClass.prototype.methodName = override(ParentClass, function methodName(par) { 33 | // Invoke inherited method from parent class 34 | methodName.inherited.apply(this, arguments); 35 | // Method code 36 | console.log('Child method implementation: methodName("' + par + '")'); 37 | }); 38 | 39 | // Child class instantiation 40 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 41 | 42 | // Check results 43 | childClassInstance.methodName('Tolstoy'); 44 | 45 | /* Output: 46 | 47 | Parent method implementation: methodName("Tolstoy") 48 | Child method implementation: methodName("Tolstoy") 49 | 50 | */ 51 | -------------------------------------------------------------------------------- /English/inheritance6.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function override(parent, fn) { 13 | fn.inherited = parent.prototype[fn.name]; 14 | return fn; 15 | } 16 | 17 | function ParentClass(par1, par2) { 18 | this.parentField1 = par1; 19 | this.parentField2 = par2; 20 | } 21 | 22 | ParentClass.prototype.methodName = function(par) { 23 | this.parentField3 = par; 24 | }; 25 | 26 | function ChildClass(par1, par2) { 27 | ChildClass.super_.apply(this, arguments); 28 | this.childField1 = par1; 29 | this.childField2 = par2; 30 | } 31 | 32 | util.inherits(ChildClass, ParentClass); 33 | 34 | ChildClass.prototype.methodName = override(ParentClass, function methodName(par) { 35 | methodName.inherited.apply(this, arguments); 36 | this.childField3 = par; 37 | }); 38 | 39 | speedTest('Instantiation test', 10000000, function() { 40 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 41 | }); 42 | 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | speedTest('Call method test', 10000000, function() { 45 | childClassInstance.methodName('Tolstoy'); 46 | }); 47 | -------------------------------------------------------------------------------- /English/inheritance7.js: -------------------------------------------------------------------------------- 1 | // Define override/inherited methods 2 | var _inherits = function (child, parent) { 3 | child.prototype = Object.create(parent && parent.prototype, { 4 | constructor: { 5 | value: child, 6 | enumerable: false, 7 | writable: true, 8 | configurable: true 9 | } 10 | }); 11 | if (parent) child.__proto__ = parent; 12 | }; 13 | 14 | // Parent class constructor 15 | var ParentClass = function ParentClass(par1, par2) { 16 | this.parentField1 = par1; 17 | this.parentField2 = par2; 18 | }; 19 | 20 | // Parent class method 21 | ParentClass.prototype.methodName = function (par) { 22 | console.log('Parent method implementation: methodName("' + par + '")'); 23 | }; 24 | 25 | // Child class constructor 26 | var ChildClass = (function () { 27 | var _ParentClass = ParentClass; 28 | var ChildClass = function ChildClass(par1, par2) { 29 | _ParentClass.call(this, par1, par2); 30 | this.childField1 = par1; 31 | this.childField2 = par2; 32 | }; 33 | 34 | // Inheritance 35 | _inherits(ChildClass, _ParentClass); 36 | 37 | // Override method in child 38 | ChildClass.prototype.methodName = function (par) { 39 | _ParentClass.prototype.methodName.call(this, par); 40 | console.log('Child method implementation: methodName("' + par + '")'); 41 | }; 42 | 43 | return ChildClass; 44 | })(); 45 | 46 | // Child class instantiation 47 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 48 | 49 | // Check results 50 | childClassInstance.methodName('Tolstoy'); 51 | 52 | /* Output: 53 | 54 | Parent method implementation: methodName("Tolstoy") 55 | Child method implementation: methodName("Tolstoy") 56 | 57 | */ 58 | -------------------------------------------------------------------------------- /English/inheritance7.speedtest.js: -------------------------------------------------------------------------------- 1 | function speedTest(caption, count, fn) { 2 | console.log(caption); 3 | var startTime = new Date().getTime(); 4 | for (var i = 0; i < count; i++) fn(); 5 | var endTime = new Date().getTime(), 6 | processingTime = endTime - startTime; 7 | console.log('Processing time: ' + processingTime + '\n'); 8 | } 9 | 10 | var _inherits = function (child, parent) { 11 | child.prototype = Object.create(parent && parent.prototype, { 12 | constructor: { 13 | value: child, 14 | enumerable: false, 15 | writable: true, 16 | configurable: true 17 | } 18 | }); 19 | if (parent) child.__proto__ = parent; 20 | }; 21 | 22 | var ParentClass = function ParentClass(par1, par2) { 23 | this.parentField1 = par1; 24 | this.parentField2 = par2; 25 | }; 26 | 27 | ParentClass.prototype.methodName = function (par) { 28 | this.parentField3 = par; 29 | }; 30 | 31 | var ChildClass = (function () { 32 | var _ParentClass = ParentClass; 33 | var ChildClass = function ChildClass(par1, par2) { 34 | _ParentClass.call(this, par1, par2); 35 | this.childField1 = par1; 36 | this.childField2 = par2; 37 | }; 38 | 39 | _inherits(ChildClass, _ParentClass); 40 | 41 | ChildClass.prototype.methodName = function (par) { 42 | _ParentClass.prototype.methodName.call(this, par); 43 | this.childField3 = par; 44 | }; 45 | 46 | return ChildClass; 47 | })(); 48 | 49 | speedTest('Instantiation test', 10000000, function() { 50 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 51 | }); 52 | 53 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 54 | speedTest('Call method test', 10000000, function() { 55 | childClassInstance.methodName('Tolstoy'); 56 | }); 57 | -------------------------------------------------------------------------------- /English/inheritance8.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Define override/inherited methods 4 | function override(child, fn) { 5 | child.prototype[fn.name] = fn; 6 | fn.inherited = child.super_.prototype[fn.name]; 7 | } 8 | 9 | // Parent class constructor 10 | function ParentClass(par1, par2) { 11 | this.parentField1 = par1; 12 | this.parentField2 = par2; 13 | } 14 | 15 | // Parent class method 16 | ParentClass.prototype.methodName = function(par) { 17 | console.log('Parent method implementation: methodName("' + par + '")'); 18 | }; 19 | 20 | // Child class constructor 21 | function ChildClass(par1, par2) { 22 | ChildClass.super_.call(this, par1, par2); 23 | this.childField1 = par1; 24 | this.childField2 = par2; 25 | } 26 | 27 | // Inheritance 28 | util.inherits(ChildClass, ParentClass); 29 | 30 | // Override method in child 31 | override(ChildClass, function methodName(par) { 32 | // Invoke inherited method from parent class 33 | methodName.inherited.call(this, par); 34 | // Method code 35 | console.log('Child method implementation: methodName("' + par + '")'); 36 | }); 37 | 38 | // Child class instantiation 39 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 40 | 41 | // Check results 42 | childClassInstance.methodName('Tolstoy'); 43 | 44 | /* Output: 45 | 46 | Parent method implementation: methodName("Tolstoy") 47 | Child method implementation: methodName("Tolstoy") 48 | 49 | */ 50 | -------------------------------------------------------------------------------- /English/inheritance8.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function override(child, fn) { 13 | child.prototype[fn.name] = fn; 14 | fn.inherited = child.super_.prototype[fn.name]; 15 | } 16 | 17 | function ParentClass(par1, par2) { 18 | this.parentField1 = par1; 19 | this.parentField2 = par2; 20 | } 21 | 22 | ParentClass.prototype.methodName = function(par) { 23 | this.parentField3 = par; 24 | }; 25 | 26 | function ChildClass(par1, par2) { 27 | ChildClass.super_.call(this, par1, par2); 28 | this.childField1 = par1; 29 | this.childField2 = par2; 30 | } 31 | 32 | util.inherits(ChildClass, ParentClass); 33 | 34 | override(ChildClass, function methodName(par) { 35 | methodName.inherited.call(this, par); 36 | this.childField3 = par; 37 | }); 38 | 39 | speedTest('Instantiation test', 10000000, function() { 40 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 41 | }); 42 | 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | speedTest('Call method test', 10000000, function() { 45 | childClassInstance.methodName('Tolstoy'); 46 | }); 47 | -------------------------------------------------------------------------------- /English/inheritance9.js: -------------------------------------------------------------------------------- 1 | // Define override/inherited methods 2 | function inherits(child, parent) { 3 | child.super_ = parent; 4 | child.prototype = Object.create(parent.prototype, { 5 | constructor: { 6 | value: child, 7 | enumerable: false, 8 | writable: true, 9 | configurable: true 10 | } 11 | }); 12 | child.override = function(fn) { 13 | child.prototype[fn.name] = fn; 14 | fn.inherited = parent.prototype[fn.name]; 15 | }; 16 | }; 17 | 18 | // Parent class constructor 19 | function ParentClass(par1, par2) { 20 | this.parentField1 = par1; 21 | this.parentField2 = par2; 22 | } 23 | 24 | // Parent class method 25 | ParentClass.prototype.methodName = function(par) { 26 | console.log('Parent method implementation: methodName("' + par + '")'); 27 | }; 28 | 29 | // Child class constructor 30 | function ChildClass(par1, par2) { 31 | ChildClass.super_.call(this, par1, par2); 32 | this.childField1 = par1; 33 | this.childField2 = par2; 34 | } 35 | 36 | // Inheritance 37 | inherits(ChildClass, ParentClass); 38 | 39 | // Override method in child 40 | ChildClass.override(function methodName(par) { 41 | // Invoke inherited method from parent class 42 | methodName.inherited.call(this, par); 43 | // Method code 44 | console.log('Child method implementation: methodName("' + par + '")'); 45 | }); 46 | 47 | // Child class instantiation 48 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 49 | 50 | // Check results 51 | childClassInstance.methodName('Tolstoy'); 52 | 53 | /* Output: 54 | 55 | Parent method implementation: methodName("Tolstoy") 56 | Child method implementation: methodName("Tolstoy") 57 | 58 | */ 59 | -------------------------------------------------------------------------------- /English/inheritance9.speedtest.js: -------------------------------------------------------------------------------- 1 | function speedTest(caption, count, fn) { 2 | console.log(caption); 3 | var startTime = new Date().getTime(); 4 | for (var i = 0; i < count; i++) fn(); 5 | var endTime = new Date().getTime(), 6 | processingTime = endTime - startTime; 7 | console.log('Processing time: ' + processingTime + '\n'); 8 | } 9 | 10 | function inherits(child, parent) { 11 | child.super_ = parent; 12 | child.prototype = Object.create(parent.prototype, { 13 | constructor: { 14 | value: child, 15 | enumerable: false, 16 | writable: true, 17 | configurable: true 18 | } 19 | }); 20 | child.override = function(fn) { 21 | child.prototype[fn.name] = fn; 22 | fn.inherited = parent.prototype[fn.name]; 23 | }; 24 | }; 25 | 26 | function ParentClass(par1, par2) { 27 | this.parentField1 = par1; 28 | this.parentField2 = par2; 29 | } 30 | 31 | ParentClass.prototype.methodName = function(par) { 32 | this.parentField3 = par; 33 | }; 34 | 35 | function ChildClass(par1, par2) { 36 | ChildClass.super_.call(this, par1, par2); 37 | this.childField1 = par1; 38 | this.childField2 = par2; 39 | } 40 | 41 | inherits(ChildClass, ParentClass); 42 | 43 | ChildClass.override(function methodName(par) { 44 | methodName.inherited.call(this, par); 45 | this.childField3 = par; 46 | }); 47 | 48 | speedTest('Instantiation test', 10000000, function() { 49 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 50 | }); 51 | 52 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 53 | speedTest('Call method test', 10000000, function() { 54 | childClassInstance.methodName('Tolstoy'); 55 | }); 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Node.js class inheritance examples 2 | ========= 3 | 4 | Supported features: 5 | * Using Node.js native method: util.inherits 6 | * Define parent and child class fields and methods 7 | * Shows how to call parent constructor from child constructor 8 | * Shows how to override parent method in child 9 | * Shows how to call parent method from overriden method in child 10 | 11 | Now inheritance8.js is the best solution among tested. 12 | -------------------------------------------------------------------------------- /Russian/inheritance1.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Определение классов 4 | 5 | function ParentClass(par1, par2) { 6 | this.parentField1 = par1; 7 | this.parentField2 = par2; 8 | } 9 | 10 | function ChildClass(par1, par2) { 11 | ChildClass.super_.apply(this, arguments); 12 | this.childField1 = par1; 13 | this.childField2 = par2; 14 | } 15 | 16 | // Наследование 17 | 18 | util.inherits(ChildClass, ParentClass); 19 | 20 | // Создание объекта дочернего класса и проверка результата 21 | 22 | var obj = new ChildClass('Hello', 'World'); 23 | console.dir({ obj: obj }); 24 | 25 | /* Консоль: 26 | 27 | { obj: 28 | { parentField1: 'Hello', 29 | parentField2: 'World', 30 | childField1: 'Hello', 31 | childField2: 'World' } } 32 | */ 33 | -------------------------------------------------------------------------------- /Russian/inheritance2.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Конструктор родительского класса 4 | function ParentClass(par1, par2) { 5 | this.parentField1 = par1; 6 | this.parentField2 = par2; 7 | } 8 | 9 | // Метод родительского класса 10 | ParentClass.prototype.parentMethod = function(par) { 11 | console.log('parentMethod("' + par + '")'); 12 | }; 13 | 14 | // Свойство родительского класса 15 | ParentClass.prototype.parentField = 'Parent field value'; 16 | 17 | // Конструктор дочернего класса 18 | function ChildClass(par1, par2) { 19 | ChildClass.super_.apply(this, arguments); 20 | this.childField1 = par1; 21 | this.childField2 = par2; 22 | } 23 | 24 | // Наследование 25 | util.inherits(ChildClass, ParentClass); 26 | 27 | // Метод дочернего класса 28 | ChildClass.prototype.childMethod = function(par) { 29 | console.log('childMethod("' + par + '")'); 30 | }; 31 | 32 | // Свойство дочернего класса 33 | ChildClass.prototype.childField = 'Child field value'; 34 | 35 | // Создание объектов от каждого класса 36 | var parentClassInstance = new ParentClass('Marcus', 'Aurelius'); 37 | var childClassInstance = new ChildClass('Yuriy', 'Gagarin'); 38 | 39 | // Проверка результатов 40 | console.dir({ 41 | parentClassInstance: parentClassInstance, 42 | childClassInstance: childClassInstance 43 | }); 44 | 45 | console.dir({ 46 | objectFieldDefinedInParent: childClassInstance.parentField1, 47 | classFieldDefinedInParent: childClassInstance.parentField, 48 | objectFieldDefinedInChild: childClassInstance.childField1, 49 | classFieldDefinedInChild: childClassInstance.childField 50 | }); 51 | 52 | parentClassInstance.parentMethod('Cartesius'); 53 | childClassInstance.childMethod('von Leibniz'); 54 | 55 | /* Консоль: 56 | 57 | { parentClassInstance: 58 | { parentField1: 'Marcus', parentField2: 'Aurelius' }, 59 | childClassInstance: 60 | { parentField1: 'Yuriy', parentField2: 'Gagarin', 61 | childField1: 'Yuriy', childField2: 'Gagarin' } } 62 | { objectFieldDefinedInParent: 'Yuriy', 63 | classFieldDefinedInParent: 'Parent field value', 64 | objectFieldDefinedInChild: 'Yuriy', 65 | classFieldDefinedInChild: 'Child field value' } 66 | parentMethod("Cartesius") 67 | childMethod("von Leibniz") 68 | 69 | */ 70 | -------------------------------------------------------------------------------- /Russian/inheritance3.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Конструктор родительского класса 4 | function ParentClass(par1, par2) { 5 | this.parentField1 = par1; 6 | this.parentField2 = par2; 7 | } 8 | 9 | // Метод родительского класса 10 | ParentClass.prototype.methodName = function(par) { 11 | console.log('Parent method implementation: methodName("' + par + '")'); 12 | }; 13 | 14 | // Конструктор дочернего класса 15 | function ChildClass(par1, par2) { 16 | ChildClass.super_.apply(this, arguments); 17 | this.childField1 = par1; 18 | this.childField2 = par2; 19 | } 20 | 21 | // Наследование 22 | util.inherits(ChildClass, ParentClass); 23 | 24 | // Переопределение метода в дочернем классе 25 | ChildClass.prototype.methodName = function(par) { 26 | // Вызов метода родительского класса 27 | ChildClass.super_.prototype.methodName.apply(this, arguments); 28 | // Собственный функционал 29 | console.log('Child method implementation: methodName("' + par + '")'); 30 | }; 31 | 32 | // Создание объекта дочернего класса 33 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 34 | 35 | // Проверка результатов 36 | childClassInstance.methodName('Tolstoy'); 37 | 38 | /* Консоль: 39 | 40 | Parent method implementation: methodName("Tolstoy") 41 | Child method implementation: methodName("Tolstoy") 42 | 43 | */ 44 | -------------------------------------------------------------------------------- /Russian/inheritance3.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function ParentClass(par1, par2) { 13 | this.parentField1 = par1; 14 | this.parentField2 = par2; 15 | } 16 | 17 | ParentClass.prototype.methodName = function(par) { 18 | this.parentField3 = par; 19 | }; 20 | 21 | function ChildClass(par1, par2) { 22 | ChildClass.super_.apply(this, arguments); 23 | this.childField1 = par1; 24 | this.childField2 = par2; 25 | } 26 | 27 | util.inherits(ChildClass, ParentClass); 28 | 29 | ChildClass.prototype.methodName = function(par) { 30 | ChildClass.super_.prototype.methodName.apply(this, arguments); 31 | this.childField3 = par; 32 | }; 33 | 34 | speedTest('Instantiation test', 10000000, function() { 35 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 36 | }); 37 | 38 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 39 | speedTest('Call method test', 10000000, function() { 40 | childClassInstance.methodName('Tolstoy'); 41 | }); 42 | -------------------------------------------------------------------------------- /Russian/inheritance4-1.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | Function.prototype.override = function(fn) { 13 | var superFunction = this; 14 | return function() { 15 | this.inherited = superFunction; 16 | return fn.apply(this, arguments); 17 | }; 18 | }; 19 | 20 | function ParentClass(par1, par2) { 21 | this.parentField1 = par1; 22 | this.parentField2 = par2; 23 | } 24 | 25 | ParentClass.prototype.methodName = function(par) { 26 | this.parentField3 = par; 27 | }; 28 | 29 | function ChildClass(par1, par2) { 30 | ChildClass.super_.apply(this, arguments); 31 | this.childField1 = par1; 32 | this.childField2 = par2; 33 | } 34 | 35 | util.inherits(ChildClass, ParentClass); 36 | 37 | ChildClass.prototype.methodName = ParentClass.prototype.methodName.override(function(par) { 38 | this.inherited(par); 39 | this.childField3 = par; 40 | }); 41 | 42 | speedTest('Instantiation test', 10000000, function() { 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | }); 45 | 46 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 47 | speedTest('Call method test', 10000000, function() { 48 | childClassInstance.methodName('Tolstoy'); 49 | }); 50 | -------------------------------------------------------------------------------- /Russian/inheritance4-2.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | Function.prototype.override = function(fn) { 13 | var superFunction = this; 14 | return function() { 15 | this.inherited = superFunction; 16 | return fn.apply(this, arguments); 17 | }; 18 | }; 19 | 20 | function ParentClass(par1, par2) { 21 | this.parentField1 = par1; 22 | this.parentField2 = par2; 23 | } 24 | 25 | ParentClass.prototype.methodName = function(par) { 26 | this.parentField3 = par; 27 | }; 28 | 29 | function ChildClass(par1, par2) { 30 | ChildClass.super_.apply(this, arguments); 31 | this.childField1 = par1; 32 | this.childField2 = par2; 33 | } 34 | 35 | util.inherits(ChildClass, ParentClass); 36 | 37 | ChildClass.prototype.methodName = ParentClass.prototype.methodName.override(function(par) { 38 | this.inherited.apply(this, arguments); 39 | this.childField3 = par; 40 | }); 41 | 42 | speedTest('Instantiation test', 10000000, function() { 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | }); 45 | 46 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 47 | speedTest('Call method test', 10000000, function() { 48 | childClassInstance.methodName('Tolstoy'); 49 | }); 50 | -------------------------------------------------------------------------------- /Russian/inheritance4.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Средство для переопределения функций 4 | Function.prototype.override = function(fn) { 5 | var superFunction = this; 6 | return function() { 7 | this.inherited = superFunction; 8 | return fn.apply(this, arguments); 9 | }; 10 | }; 11 | 12 | // Конструктор родительского класса 13 | function ParentClass(par1, par2) { 14 | this.parentField1 = par1; 15 | this.parentField2 = par2; 16 | } 17 | 18 | // Метод родительского класса 19 | ParentClass.prototype.methodName = function(par) { 20 | console.log('Parent method implementation: methodName("' + par + '")'); 21 | }; 22 | 23 | // Конструктор дочернего класса 24 | function ChildClass(par1, par2) { 25 | ChildClass.super_.apply(this, arguments); 26 | this.childField1 = par1; 27 | this.childField2 = par2; 28 | } 29 | 30 | // Наследование 31 | util.inherits(ChildClass, ParentClass); 32 | 33 | // Переопределение метода в дочернем классе 34 | ChildClass.prototype.methodName = ParentClass.prototype.methodName.override(function(par) { 35 | // Вызов метода родительского класса 36 | this.inherited(par); // или this.inherited.apply(this, arguments); 37 | // Собственный функционал 38 | console.log('Child method implementation: methodName("' + par + '")'); 39 | }); 40 | 41 | // Создание объекта дочернего класса 42 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 43 | 44 | // Проверка результатов 45 | childClassInstance.methodName('Tolstoy'); 46 | 47 | /* Консоль: 48 | 49 | Parent method implementation: methodName("Tolstoy") 50 | Child method implementation: methodName("Tolstoy") 51 | 52 | */ 53 | -------------------------------------------------------------------------------- /Russian/inheritance5.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Средство для переопределения функций 4 | function override(fn) { 5 | var name = fn.name; 6 | return function() { 7 | this.inherited = this.constructor.super_.prototype[name]; 8 | return fn.apply(this, arguments); 9 | } 10 | } 11 | 12 | // Конструктор родительского класса 13 | function ParentClass(par1, par2) { 14 | this.parentField1 = par1; 15 | this.parentField2 = par2; 16 | } 17 | 18 | // Метод родительского класса 19 | ParentClass.prototype.methodName = function(par) { 20 | console.log('Parent method implementation: methodName("' + par + '")'); 21 | }; 22 | 23 | // Конструктор дочернего класса 24 | function ChildClass(par1, par2) { 25 | ChildClass.super_.apply(this, arguments); 26 | this.childField1 = par1; 27 | this.childField2 = par2; 28 | } 29 | 30 | // Наследование 31 | util.inherits(ChildClass, ParentClass); 32 | 33 | // Переопределение метода в дочернем классе 34 | ChildClass.prototype.methodName = override(function methodName(par) { 35 | // Вызов метода родительского класса 36 | this.inherited(par); // или this.inherited.apply(this, arguments); 37 | // Собственный функционал 38 | console.log('Child method implementation: methodName("' + par + '")'); 39 | }); 40 | 41 | // Создание объекта дочернего класса 42 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 43 | 44 | // Проверка результатов 45 | childClassInstance.methodName('Tolstoy'); 46 | 47 | /* Консоль: 48 | 49 | Parent method implementation: methodName("Tolstoy") 50 | Child method implementation: methodName("Tolstoy") 51 | 52 | */ 53 | -------------------------------------------------------------------------------- /Russian/inheritance5.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function override(fn) { 13 | var name = fn.name; 14 | return function() { 15 | this.inherited = this.constructor.super_.prototype[name]; 16 | return fn.apply(this, arguments); 17 | } 18 | } 19 | 20 | function ParentClass(par1, par2) { 21 | this.parentField1 = par1; 22 | this.parentField2 = par2; 23 | } 24 | 25 | ParentClass.prototype.methodName = function(par) { 26 | this.parentField3 = par; 27 | }; 28 | 29 | function ChildClass(par1, par2) { 30 | ChildClass.super_.apply(this, arguments); 31 | this.childField1 = par1; 32 | this.childField2 = par2; 33 | } 34 | 35 | util.inherits(ChildClass, ParentClass); 36 | 37 | ChildClass.prototype.methodName = override(function methodName(par) { 38 | this.inherited(par); // или this.inherited.apply(this, arguments); 39 | this.childField3 = par; 40 | }); 41 | 42 | speedTest('Instantiation test', 10000000, function() { 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | }); 45 | 46 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 47 | speedTest('Call method test', 10000000, function() { 48 | childClassInstance.methodName('Tolstoy'); 49 | }); 50 | -------------------------------------------------------------------------------- /Russian/inheritance6.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Средство для переопределения функций 4 | function override(parent, fn) { 5 | fn.inherited = parent.prototype[fn.name]; 6 | return fn; 7 | } 8 | 9 | // Конструктор родительского класса 10 | function ParentClass(par1, par2) { 11 | this.parentField1 = par1; 12 | this.parentField2 = par2; 13 | } 14 | 15 | // Метод родительского класса 16 | ParentClass.prototype.methodName = function(par) { 17 | console.log('Parent method implementation: methodName("' + par + '")'); 18 | }; 19 | 20 | // Конструктор дочернего класса 21 | function ChildClass(par1, par2) { 22 | ChildClass.super_.apply(this, arguments); 23 | this.childField1 = par1; 24 | this.childField2 = par2; 25 | } 26 | 27 | // Наследование 28 | util.inherits(ChildClass, ParentClass); 29 | 30 | // Переопределение метода в дочернем классе 31 | ChildClass.prototype.methodName = override(ParentClass, function methodName(par) { 32 | // Вызов метода родительского класса 33 | methodName.inherited.apply(this, arguments); // methodName.inherited(par); 34 | // Собственный функционал 35 | console.log('Child method implementation: methodName("' + par + '")'); 36 | }); 37 | 38 | // Создание объекта дочернего класса 39 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 40 | 41 | // Проверка результатов 42 | childClassInstance.methodName('Tolstoy'); 43 | 44 | /* Консоль: 45 | 46 | Parent method implementation: methodName("Tolstoy") 47 | Child method implementation: methodName("Tolstoy") 48 | 49 | */ 50 | -------------------------------------------------------------------------------- /Russian/inheritance6.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function override(parent, fn) { 13 | fn.inherited = parent.prototype[fn.name]; 14 | return fn; 15 | } 16 | 17 | function ParentClass(par1, par2) { 18 | this.parentField1 = par1; 19 | this.parentField2 = par2; 20 | } 21 | 22 | ParentClass.prototype.methodName = function(par) { 23 | this.parentField3 = par; 24 | }; 25 | 26 | function ChildClass(par1, par2) { 27 | ChildClass.super_.apply(this, arguments); 28 | this.childField1 = par1; 29 | this.childField2 = par2; 30 | } 31 | 32 | util.inherits(ChildClass, ParentClass); 33 | 34 | ChildClass.prototype.methodName = override(ParentClass, function methodName(par) { 35 | methodName.inherited.apply(this, arguments); // methodName.inherited(par); 36 | this.childField3 = par; 37 | }); 38 | 39 | speedTest('Instantiation test', 10000000, function() { 40 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 41 | }); 42 | 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | speedTest('Call method test', 10000000, function() { 45 | childClassInstance.methodName('Tolstoy'); 46 | }); 47 | -------------------------------------------------------------------------------- /Russian/inheritance7.js: -------------------------------------------------------------------------------- 1 | var _inherits = function (child, parent) { 2 | child.prototype = Object.create(parent && parent.prototype, { 3 | constructor: { 4 | value: child, 5 | enumerable: false, 6 | writable: true, 7 | configurable: true 8 | } 9 | }); 10 | if (parent) child.__proto__ = parent; 11 | }; 12 | 13 | var ParentClass = function ParentClass(par1, par2) { 14 | this.parentField1 = par1; 15 | this.parentField2 = par2; 16 | }; 17 | 18 | ParentClass.prototype.methodName = function (par) { 19 | console.log('Parent method implementation: methodName("' + par + '")'); 20 | }; 21 | 22 | var ChildClass = (function () { 23 | var _ParentClass = ParentClass; 24 | var ChildClass = function ChildClass(par1, par2) { 25 | _ParentClass.call(this, par1, par2); 26 | this.childField1 = par1; 27 | this.childField2 = par2; 28 | }; 29 | 30 | _inherits(ChildClass, _ParentClass); 31 | 32 | ChildClass.prototype.methodName = function (par) { 33 | _ParentClass.prototype.methodName.call(this, par); 34 | console.log('Child method implementation: methodName("' + par + '")'); 35 | }; 36 | 37 | return ChildClass; 38 | })(); 39 | 40 | // Создание объекта дочернего класса 41 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 42 | 43 | // Проверка результатов 44 | childClassInstance.methodName('Tolstoy'); 45 | 46 | /* Консоль: 47 | 48 | Parent method implementation: methodName("Tolstoy") 49 | Child method implementation: methodName("Tolstoy") 50 | 51 | */ 52 | -------------------------------------------------------------------------------- /Russian/inheritance7.speedtest.js: -------------------------------------------------------------------------------- 1 | function speedTest(caption, count, fn) { 2 | console.log(caption); 3 | var startTime = new Date().getTime(); 4 | for (var i = 0; i < count; i++) fn(); 5 | var endTime = new Date().getTime(), 6 | processingTime = endTime - startTime; 7 | console.log('Processing time: ' + processingTime + '\n'); 8 | } 9 | 10 | var _inherits = function (child, parent) { 11 | child.prototype = Object.create(parent && parent.prototype, { 12 | constructor: { 13 | value: child, 14 | enumerable: false, 15 | writable: true, 16 | configurable: true 17 | } 18 | }); 19 | if (parent) child.__proto__ = parent; 20 | }; 21 | 22 | var ParentClass = function ParentClass(par1, par2) { 23 | this.parentField1 = par1; 24 | this.parentField2 = par2; 25 | }; 26 | 27 | ParentClass.prototype.methodName = function (par) { 28 | this.parentField3 = par; 29 | }; 30 | 31 | var ChildClass = (function () { 32 | var _ParentClass = ParentClass; 33 | var ChildClass = function ChildClass(par1, par2) { 34 | _ParentClass.call(this, par1, par2); 35 | this.childField1 = par1; 36 | this.childField2 = par2; 37 | }; 38 | 39 | _inherits(ChildClass, _ParentClass); 40 | 41 | ChildClass.prototype.methodName = function (par) { 42 | _ParentClass.prototype.methodName.call(this, par); 43 | this.childField3 = par; 44 | }; 45 | 46 | return ChildClass; 47 | })(); 48 | 49 | speedTest('Instantiation test', 10000000, function() { 50 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 51 | }); 52 | 53 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 54 | speedTest('Call method test', 10000000, function() { 55 | childClassInstance.methodName('Tolstoy'); 56 | }); 57 | -------------------------------------------------------------------------------- /Russian/inheritance8.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Средство для переопределения функций 4 | function override(child, fn) { 5 | child.prototype[fn.name] = fn; 6 | fn.inherited = child.super_.prototype[fn.name]; 7 | } 8 | 9 | // Конструктор родительского класса 10 | function ParentClass(par1, par2) { 11 | this.parentField1 = par1; 12 | this.parentField2 = par2; 13 | } 14 | 15 | // Метод родительского класса 16 | ParentClass.prototype.methodName = function(par) { 17 | console.log('Parent method implementation: methodName("' + par + '")'); 18 | }; 19 | 20 | // Конструктор дочернего класса 21 | function ChildClass(par1, par2) { 22 | ChildClass.super_.call(this, par1, par2); 23 | this.childField1 = par1; 24 | this.childField2 = par2; 25 | } 26 | 27 | // Наследование 28 | util.inherits(ChildClass, ParentClass); 29 | 30 | // Переопределение метода в дочернем классе 31 | override(ChildClass, function methodName(par) { 32 | // Вызов метода родительского класса 33 | methodName.inherited.call(this, par); 34 | // Собственный функционал 35 | console.log('Child method implementation: methodName("' + par + '")'); 36 | }); 37 | 38 | // Создание объекта дочернего класса 39 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 40 | 41 | // Проверка результатов 42 | childClassInstance.methodName('Tolstoy'); 43 | 44 | /* Консоль: 45 | 46 | Parent method implementation: methodName("Tolstoy") 47 | Child method implementation: methodName("Tolstoy") 48 | 49 | */ 50 | -------------------------------------------------------------------------------- /Russian/inheritance8.speedtest.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | function speedTest(caption, count, fn) { 4 | console.log(caption); 5 | var startTime = new Date().getTime(); 6 | for (var i = 0; i < count; i++) fn(); 7 | var endTime = new Date().getTime(), 8 | processingTime = endTime - startTime; 9 | console.log('Processing time: ' + processingTime + '\n'); 10 | } 11 | 12 | function override(child, fn) { 13 | child.prototype[fn.name] = fn; 14 | fn.inherited = child.super_.prototype[fn.name]; 15 | } 16 | 17 | function ParentClass(par1, par2) { 18 | this.parentField1 = par1; 19 | this.parentField2 = par2; 20 | } 21 | 22 | ParentClass.prototype.methodName = function(par) { 23 | this.parentField3 = par; 24 | }; 25 | 26 | function ChildClass(par1, par2) { 27 | ChildClass.super_.call(this, par1, par2); 28 | this.childField1 = par1; 29 | this.childField2 = par2; 30 | } 31 | 32 | util.inherits(ChildClass, ParentClass); 33 | 34 | override(ChildClass, function methodName(par) { 35 | methodName.inherited.call(this, par); 36 | this.childField3 = par; 37 | }); 38 | 39 | speedTest('Instantiation test', 10000000, function() { 40 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 41 | }); 42 | 43 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 44 | speedTest('Call method test', 10000000, function() { 45 | childClassInstance.methodName('Tolstoy'); 46 | }); 47 | -------------------------------------------------------------------------------- /Russian/inheritance9.js: -------------------------------------------------------------------------------- 1 | // Средство для переопределения функций 2 | function inherits(child, parent) { 3 | child.super_ = parent; 4 | child.prototype = Object.create(parent.prototype, { 5 | constructor: { 6 | value: child, 7 | enumerable: false, 8 | writable: true, 9 | configurable: true 10 | } 11 | }); 12 | child.override = function(fn) { 13 | child.prototype[fn.name] = fn; 14 | fn.inherited = parent.prototype[fn.name]; 15 | }; 16 | }; 17 | 18 | // Конструктор родительского класса 19 | function ParentClass(par1, par2) { 20 | this.parentField1 = par1; 21 | this.parentField2 = par2; 22 | } 23 | 24 | // Метод родительского класса 25 | ParentClass.prototype.methodName = function(par) { 26 | console.log('Parent method implementation: methodName("' + par + '")'); 27 | }; 28 | 29 | // Конструктор дочернего класса 30 | function ChildClass(par1, par2) { 31 | ChildClass.super_.call(this, par1, par2); 32 | this.childField1 = par1; 33 | this.childField2 = par2; 34 | } 35 | 36 | // Наследование 37 | inherits(ChildClass, ParentClass); 38 | 39 | // Переопределение метода в дочернем классе 40 | ChildClass.override(function methodName(par) { 41 | // Вызов метода родительского класса 42 | methodName.inherited.call(this, par); 43 | // Собственный функционал 44 | console.log('Child method implementation: methodName("' + par + '")'); 45 | }); 46 | 47 | // Создание объекта дочернего класса 48 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 49 | 50 | // Проверка результатов 51 | childClassInstance.methodName('Tolstoy'); 52 | 53 | /* Консоль: 54 | 55 | Parent method implementation: methodName("Tolstoy") 56 | Child method implementation: methodName("Tolstoy") 57 | 58 | */ 59 | -------------------------------------------------------------------------------- /Russian/inheritance9.speedtest.js: -------------------------------------------------------------------------------- 1 | function speedTest(caption, count, fn) { 2 | console.log(caption); 3 | var startTime = new Date().getTime(); 4 | for (var i = 0; i < count; i++) fn(); 5 | var endTime = new Date().getTime(), 6 | processingTime = endTime - startTime; 7 | console.log('Processing time: ' + processingTime + '\n'); 8 | } 9 | 10 | function inherits(child, parent) { 11 | child.super_ = parent; 12 | child.prototype = Object.create(parent.prototype, { 13 | constructor: { 14 | value: child, 15 | enumerable: false, 16 | writable: true, 17 | configurable: true 18 | } 19 | }); 20 | child.override = function(fn) { 21 | child.prototype[fn.name] = fn; 22 | fn.inherited = parent.prototype[fn.name]; 23 | }; 24 | }; 25 | 26 | function ParentClass(par1, par2) { 27 | this.parentField1 = par1; 28 | this.parentField2 = par2; 29 | } 30 | 31 | ParentClass.prototype.methodName = function(par) { 32 | this.parentField3 = par; 33 | }; 34 | 35 | function ChildClass(par1, par2) { 36 | ChildClass.super_.call(this, par1, par2); 37 | this.childField1 = par1; 38 | this.childField2 = par2; 39 | } 40 | 41 | inherits(ChildClass, ParentClass); 42 | 43 | ChildClass.override(function methodName(par) { 44 | methodName.inherited.call(this, par); 45 | this.childField3 = par; 46 | }); 47 | 48 | speedTest('Instantiation test', 10000000, function() { 49 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 50 | }); 51 | 52 | var childClassInstance = new ChildClass('Lev', 'Nikolayevich'); 53 | speedTest('Call method test', 10000000, function() { 54 | childClassInstance.methodName('Tolstoy'); 55 | }); 56 | -------------------------------------------------------------------------------- /Russian/tree.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | 3 | // Средство для переопределения функций 4 | function override(child, fn) { 5 | child.prototype[fn.name] = fn; 6 | fn.inherited = child.super_.prototype[fn.name]; 7 | } 8 | 9 | // Конструктор родительского класса 10 | function ParentClass(par1, par2) { 11 | this.parentField1 = par1; 12 | this.parentField2 = par2; 13 | } 14 | 15 | // Метод родительского класса 16 | ParentClass.prototype.methodName = function(par) { 17 | console.log('Parent method implementation: methodName("' + par + '")'); 18 | }; 19 | 20 | // Конструктор дочернего класса 21 | function ChildClass(par1, par2) { 22 | ChildClass.super_.call(this, par1, par2); 23 | this.childField1 = par1; 24 | this.childField2 = par2; 25 | } 26 | 27 | // Наследование 28 | util.inherits(ChildClass, ParentClass); 29 | 30 | // Переопределение метода в дочернем классе 31 | override(ChildClass, function methodName(par) { 32 | // Вызов метода родительского класса 33 | methodName.inherited.call(this, par); 34 | // Собственный функционал 35 | console.log('Child method implementation: methodName("' + par + '")'); 36 | }); 37 | 38 | // Конструктор очень дочернего класса 39 | function VeryChildClass(par1, par2) { 40 | VeryChildClass.super_.call(this, par1, par2); 41 | this.childField1 = par1; 42 | this.childField2 = par2; 43 | } 44 | 45 | // Наследование 46 | util.inherits(VeryChildClass, ChildClass); 47 | 48 | // Переопределение метода в очень дочернем классе 49 | override(VeryChildClass, function methodName(par) { 50 | // Вызов метода родительского класса 51 | methodName.inherited.call(this, par); 52 | // Собственный функционал 53 | console.log('Very child method implementation: methodName("' + par + '")'); 54 | }); 55 | 56 | // Создание объекта дочернего класса 57 | var veryChildClassInstance = new VeryChildClass('Lev', 'Nikolayevich'); 58 | 59 | // Проверка результатов 60 | veryChildClassInstance.methodName('Tolstoy'); 61 | 62 | /* Консоль: 63 | 64 | Parent method implementation: methodName("Tolstoy") 65 | Child method implementation: methodName("Tolstoy") 66 | Very child method implementation: methodName("Tolstoy") 67 | 68 | */ 69 | --------------------------------------------------------------------------------