Skip to content

Instantly share code, notes, and snippets.

@morganwilde
Created November 20, 2015 04:38
Show Gist options
  • Save morganwilde/47a9a79dcca1a54186b7 to your computer and use it in GitHub Desktop.
Save morganwilde/47a9a79dcca1a54186b7 to your computer and use it in GitHub Desktop.
family.js
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
margin: 0px;
}
</style>
</head>
<body>
<script>
// Parent
function Point()
{
this.components = [];
}
Point.prototype.initWithComponents = function(components)
{
this.components = [];
var component = null;
for (var i = 0; component = components[i]; i++) {
this.components.push(component);
}
return this;
};
Point.prototype.stringValue = function()
{
var stringValue = '(';
var component = null;
for (var i = 0; component = this.components[i]; i++) {
stringValue += (i == 0 ? '' : ', ') + component;
}
stringValue += ')';
return stringValue;
};
// Child
function Point3D()
{
Point.call(this);
}
Point3D.prototype = Object.create(Point.prototype);
Point3D.prototype.constructor = Point3D;
// Initializers
Point3D.prototype.initWithXYZ = function(x, y, z)
{
return this.initWithComponents([x, y, z]);
};
// Getters
Point3D.prototype.getX = function(){ return this.components[0]; };
Point3D.prototype.getY = function(){ return this.components[1]; };
Point3D.prototype.getZ = function(){ return this.components[2]; };
// Setters
Point3D.prototype.setX = function(x) { this.components[0] = x; };
Point3D.prototype.setY = function(y) { this.components[1] = y; };
Point3D.prototype.setZ = function(z) { this.components[2] = z; };
Point3D.prototype.stringValue = function()
{
return 'Point3D -> ' + Point.prototype.stringValue.call(this);
};
window.onload = function()
{
// var p = new Point().initWithComponents([1, 2, 3]);
var p = new Point3D().initWithXYZ(1, 2, 3);
console.log(p.stringValue());
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment