Created
April 21, 2013 15:27
-
-
Save JamieKnight/5429993 to your computer and use it in GitHub Desktop.
JS prototypes and inheritance. testing argument to preserve require argument tests.
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
<!doctype> | |
<html> | |
<head> | |
</head> | |
<body> | |
<h1> Inheretence testing</h1> | |
<script type="text/javascript"> | |
//base object | |
function panel(name){ | |
if ( arguments.length > 0 ){ | |
if ( typeof name == 'undefined') { throw "invalid input"; } | |
console.log('panel constructor'); | |
this.name = name; | |
} | |
} | |
panel.prototype.sayHello = function(){ | |
return "hello " + this.name; | |
} | |
//extended object | |
function stationPanel(name){ | |
panel.apply(this, arguments); | |
} | |
stationPanel.prototype = new panel; | |
stationPanel.prototype.sayHello = function() { | |
return this.name + " says hello"; | |
} | |
//Panels and Stations panels. | |
p = new panel('jamie') | |
console.log(p.sayHello()); | |
s = new stationPanel('station'); | |
console.log(s.sayHello()); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment