Created
August 8, 2014 07:19
-
-
Save jaywon/381f8a88d9a4894b3c2e to your computer and use it in GitHub Desktop.
Method inheritance from base class called from sub classes and switching context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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