Skip to content

Instantly share code, notes, and snippets.

@jwestbrook
Last active December 19, 2015 13:19
Show Gist options
  • Save jwestbrook/5961046 to your computer and use it in GitHub Desktop.
Save jwestbrook/5961046 to your computer and use it in GitHub Desktop.
PrototypeJS Class Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Prototype Classes</title>
<script src="//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
<script type="text/javascript">
var myClass = Class.create(
{
initialize : function(option)
{
this.options = (option ? option : 0);
this.testme();
},
testme : function()
{
this.options++;
},
showme : function()
{
alert(this.options);
return this.options;
}
});
var myChildClass = Class.create(myClass,
{
initialize : function($super,option)
{
$super(option);
// the child class needs option's default value at 150 or whatever is
// passed so run the parent initialize first and then redefine the
// option property
this.option = (option ? option : 150);
// you can still run methods in the parent that are not overridden in
// the child
this.testme();
}
});
document.observe('dom:loaded',function(){
var theClass = new myClass(200);
var child = new myChildClass();
$('show_results').observe('click',function(e){
theClass.showme();
e.stop();
});
$('show_child_results').observe('click',function(e){
child.showme();
e.stop();
});
});
</script>
</head>
<body>
<button id="show_results">Show Results</button><br />
<button id="show_child_results">Show Child Results</button>
</body>
<!-- More Extensions at http://github.com/jwestbrook -->
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment