Skip to content

Instantly share code, notes, and snippets.

@jaywon
Created August 8, 2014 07:19
Show Gist options
  • Save jaywon/381f8a88d9a4894b3c2e to your computer and use it in GitHub Desktop.
Save jaywon/381f8a88d9a4894b3c2e to your computer and use it in GitHub Desktop.
Method inheritance from base class called from sub classes and switching context
//Main Class
//Create base div class
function Div(){
this.color = null;
this.htmlElement = null;
this.draw = function (){
this.htmlElement = $("<div>"+this.color+"</div>");
$('body').append(this.htmlElement);
};
}
//Create sub classes
function RedDiv(){
Div.call(this);
this.baseClassDraw = this.draw;
this.color = "#FF0000";
}
RedDiv.prototype = new Div();
RedDiv.prototype.draw = function(){
this.baseClassDraw.call(this);
};
function BlueDiv(){
Div.call(this);
this.baseClassDraw = this.draw;
this.color = "#0000FF";
}
BlueDiv.prototype = new Div();
BlueDiv.prototype.draw = function(){
this.baseClassDraw.call(this);
};
//Create an instance to proove it works
$( document ).ready(function (){
var newredDiv = new RedDiv();
var newblueDiv = new BlueDiv();
newredDiv.draw();
newblueDiv.draw();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment