Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Inheritance [closed]

I am trying to implement inheritance in javascript. I came up with following minimal code to support it.

function Base(){     this.call = function(handler, args){       handler.call(this, args);     } }  Base.extend = function(child, parent){     parent.apply(child);     child.base = new parent;     child.base.child = child; } 

Experts, please let me know if this will be sufficient or any other important issue I may have missed. Based on similar issues faced please suggest other changes.

Here is complete test script:

function Base(){     this.call = function(handler, args){       handler.call(this, args);     }     this.superalert = function(){         alert('tst');     } }  Base.extend = function(child, parent){     parent.apply(child);     child.base = new parent;     child.base.child = child; }  function Child(){     Base.extend(this, Base);     this.width = 20;     this.height = 15;     this.a = ['s',''];     this.alert = function(){         alert(this.a.length);         alert(this.height);     } }  function Child1(){     Base.extend(this, Child);     this.depth = 'depth';     this.height = 'h';     this.alert = function(){         alert(this.height); // display current object height         alert(this.a.length); // display parents array length         this.call(this.base.alert);            // explicit call to parent alert with current objects value         this.call(this.base.superalert);            // explicit call to grandparent, parent does not have method          this.base.alert(); // call parent without overriding values     } }  var v = new Child1(); v.alert(); alert(v.height); alert(v.depth); 
like image 353
hungryMind Avatar asked Sep 20 '11 14:09

hungryMind


People also ask

How does JavaScript inheritance work?

When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.

What are the closures in JavaScript?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

Does JavaScript have inheritance?

Inheritance is an important concept in object oriented programming. In the classical inheritance, methods from base class get copied into derived class. In JavaScript, inheritance is supported by using prototype object.

Why closures are used in JavaScript?

In JavaScript, closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can't get at the data from an outside scope except through the object's privileged methods.


1 Answers

To implement javascript inheritance in ECMAScript 5 you can define the prototype of an object and use Object.create to inherit. You can also add/override properties as much as you want.

Example:

/**  * Transform base class  */ function Transform() {     this.type = "2d"; }  Transform.prototype.toString = function() {     return "Transform"; }  /**  * Translation class.  */ function Translation(x, y) {     // Parent constructor     Transform.call(this);      // Public properties     this.x = x;     this.y = y; }  // Inheritance Translation.prototype = Object.create(Transform.prototype);  // Override Translation.prototype.toString = function() {     return Transform.prototype.toString() + this.type + " Translation " + this.x + ":" + this.y; }  /**  * Rotation class.  */ function Rotation(angle) {     // Parent constructor     Transform.call(this);      // Public properties     this.angle = angle; }  // Inheritance Rotation.prototype = Object.create(Transform.prototype);  // Override Rotation.prototype.toString = function() {     return Transform.prototype.toString() + this.type + " Rotation " + this.angle; }  // Tests translation = new Translation(10, 15);  console.log(translation instanceof Transform); // true console.log(translation instanceof Translation); // true console.log(translation instanceof Rotation); // false  console.log(translation.toString()) // Transform2d Translation 10:15 
like image 142
Lorenzo Polidori Avatar answered Oct 08 '22 00:10

Lorenzo Polidori