Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A simple function that enables for java-like object inheritance in javascript

License

Notifications You must be signed in to change notification settings

hk6an6/javalikejavascriptinheritance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

java-like javascript inheritance

A simple function that enables for java-like object inheritance in javascript!

Features:

  • Superclass constructors are executed before subclass constructor code runs
  • Superclass constructor parameters can be provided to subclass constructor
  • Subclass constructor may use additional parameters
  • Overriden superclass member functions are available via .prototype.uber
  • Superclass instance variables are injected into subclass instances (because the superclass constructor is executed before the subclass constructor takes place)

Sample code for class inheritance

var Extend = require('./Extend.js');
function Shape() {
    this.name = "shape";
    console.log('Shape Constructor ran');
};
Shape.prototype.toString = function(){
    console.log('<a shape>');
};
function TwoDShape() {
    this.name = "a 2D shape";
    console.log('TwoDShape Constructor ran');
};
TwoDShape = Extend(Shape, TwoDShape);
//declare instance methods only after having called Extend
TwoDShape.prototype.toString = function () {
    //tap into Shape's "toString" implementation
    return TwoDShape.prototype.uber.toString.call(this) + " " + this.name;
};
function ThreeDShape() {
    this.name = "a 3D shape";
    console.log('ThreeDShape Constructor ran');
};
ThreeDShape = Extend(TwoDShape, ThreeDShape);
//declare instance methods only after having called Extend
ThreeDShape.prototype.toString = function () {
    //tap into TwoDShape's "toString" implementation
    return ThreeDShape.prototype.uber.toString.call(this) + " " + this.name;
};

var a3DShape = new ThreeDShape();
//the following line prints: "Shape Constructor ran... TwoDShape Constructor ran.... ThreeDShape Constructor ran"

a3DShape.toString();
//prints: "<a shape> a 3D shape a 3D shape"

About

A simple function that enables for java-like object inheritance in javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors