Skip to content

Instantly share code, notes, and snippets.

@JamieKnight
Created April 21, 2013 15:27
Show Gist options
  • Save JamieKnight/5429993 to your computer and use it in GitHub Desktop.
Save JamieKnight/5429993 to your computer and use it in GitHub Desktop.
JS prototypes and inheritance. testing argument to preserve require argument tests.
<!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