I'm trying to use a method from a parent class inside the child class. In other languages, you would just have to use extends
and all the methods from the parent can be used in the child, but in JavaScript, it seems to be different.
function Svg() {
this.align = function(value) {
if(value === 'left') return 0;
}
}
function Graph() {
// I want to align text to the left
console.log('<text x="' + align('left') + '"></text>');
}
graph = new Graph();
Graph.prototype = new Svg();
Graph.prototype.align.call();
I do understand that the code below doesn't necessarily 'extend' like in other OOP languages. But it does take another function/class as a property - for which you can call it's methods directly. Furthermore, I haven't used JavaScript prototyping for this demo.
<script>
function Svg() {
this.align = function( align ) {
if( align == 'left')
return 'left';
else
return 0;
}
}
function Graph() {
this.Svg = new Svg();
// I want to align text to the left
this.AlignLeft = function( direction ) {
console.log('<text x="' + this.Svg.align( direction ) + '"></text>');
}
}
graph = new Graph();
graph.AlignLeft('left'); //console.log <text x="left"></text>
</script>
Fiddle: http://jsfiddle.net/cBMQg/11/