Created
July 12, 2010 20:28
-
-
Save anoras/473008 to your computer and use it in GitHub Desktop.
Example of too clever JavaScript code for my JavaZone 2010 talk.
This file contains hidden or 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
| Object.prototype.inherits = function(base, args) { | |
| if (arguments.length > 1) { | |
| base.apply(this, Array.prototype.slice.call(arguments, 1)); | |
| } else { | |
| base.call(this); | |
| } | |
| }; | |
| Function.prototype.inherits = function(base){ | |
| this.prototype = new base(); | |
| this.prototype.constructor = this; | |
| }; |
This file contains hidden or 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
| function Interface(name, methods) { | |
| this.name = name; | |
| this.methods = Array.prototype.slice.call(arguments, 1); | |
| }; | |
| Interface.implemented = function(object) { | |
| if (arguments.length < 2) { | |
| throw new Error('Interface.implemented expects at least 2 arguments.'); | |
| } | |
| for (var i = 1; i < arguments.length; i++) { | |
| var interfaze = arguments[i]; | |
| if (interfaze.constructor !== Interface) { | |
| throw new Error(interfaze.constructor + ' is not an interface.'); | |
| } | |
| for (var j = 0; j < interfaze.methods.length; j++) { | |
| var method = interfaze.methods[j]; | |
| if (!object[method]) { | |
| if (typeof object[method] !== 'function') { | |
| throw new Error(object + ' does not implement the "' + interfaze.name + '" interface. ' + | |
| 'Method "' + method + '" was not found.'); | |
| } | |
| } | |
| } | |
| } | |
| }; |
This file contains hidden or 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
| // If you're wondering what the load() and print() methods are, check out Squirrelfish. It is super awesome. | |
| load('./Inheritance.js'); | |
| load('./Interface.js'); | |
| var Colorful = new Interface('Colorful', 'getR', 'getG', 'getB'); | |
| function Color(r, g, b) { | |
| Interface.implemented(this, Colorful); | |
| this.r = r; | |
| this.g = g; | |
| this.b = b; | |
| }; | |
| Color.prototype.getR = function() { | |
| return this.r; | |
| }; | |
| Color.prototype.setR = function(r) { | |
| return this.r = r; | |
| }; | |
| Color.prototype.getG = function() { | |
| return this.g; | |
| }; | |
| Color.prototype.setG = function(g) { | |
| return this.g = g; | |
| }; | |
| Color.prototype.getB = function() { | |
| return this.b; | |
| }; | |
| Color.prototype.setB = function(b) { | |
| return this.b = b; | |
| }; | |
| Color.prototype.blend = function(other_color) { | |
| Interface.implemented(other_color, Colorful); | |
| return new Color( | |
| Math.floor(other_color.getR() + this.getR() / 2), | |
| Math.floor(other_color.getG() + this.getG() / 2), | |
| Math.floor(other_color.getB() + this.getB() / 2) | |
| ); | |
| }; | |
| Color.prototype.toString = function() { | |
| return '(' + this.getR() + ', ' + this.getG() + ', ' + this.getB() + ')'; | |
| }; | |
| var SeeThrough = new Interface('SeeThrough', 'getAlpha'); | |
| TransparentColor.inherits(Color); | |
| function TransparentColor(r,g,b,a) { | |
| Interface.implemented(this, Colorful); | |
| Interface.implemented(this, SeeThrough); | |
| this.inherits(Color, r, g, b); | |
| this.alpha = a; | |
| }; | |
| TransparentColor.prototype.getAlpha = function() { | |
| return this.alpha; | |
| }; | |
| TransparentColor.prototype.setAlpha = function(a) { | |
| this.alpha = a; | |
| }; | |
| TransparentColor.prototype.blend = function(other_color) { | |
| Interface.implemented(other_color, Colorful); | |
| return new TransparentColor( | |
| Math.floor(other_color.getR() + this.getR() / 2), | |
| Math.floor(other_color.getG() + this.getG() / 2), | |
| Math.floor(other_color.getB() + this.getB() / 2), | |
| typeof other_color === TransparentColor ? Math.floor(other_color.getAlpha() + this.getAlpha() / 2) : this.getAlpha() | |
| ); | |
| }; | |
| TransparentColor.prototype.toString = function() { | |
| return '(' + this.getR() + ', ' + this.getG() + ', ' + this.getB() + ': ' + this.getAlpha() + '%)'; | |
| }; | |
| var red = new Color(255,0,0); | |
| var blue_glass = new TransparentColor(0,0,255,50); | |
| var pink_glass = blue_glass.blend(red); | |
| print(red.toString()); | |
| print(blue_glass.toString()); | |
| print(pink_glass.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment