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
| 5/25/2013 |
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 randomColor() { | |
| return 'rgb(' + | |
| parseInt(Math.random() * 256, 10) + ',' + | |
| parseInt(Math.random() * 256, 10) + ',' + | |
| parseInt(Math.random() * 256, 10) + ');'; | |
| } | |
| function randomKeyframe (keyframes) { | |
| var duration = 0; | |
| for (var i = 0; i < keyframes; i++) { |
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
| @jeremyckahn | |
| > This works in ES3 and `Object.create` does not | |
| Actually, the [non-ES5 polyfilly for `Object.create()`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create#Polyfill) is: | |
| ```js | |
| Object.create = function(o) { | |
| function F(){} | |
| F.prototype = o; |
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
| I don't like this pattern for a variety of reasons, which I went into in detail in a [3-part](http://davidwalsh.name/javascript-objects) [blog](http://davidwalsh.name/javascript-objects-distractions) [post](http://davidwalsh.name/javascript-objects-deconstruction) series, titled "JS Objects". | |
| The constructor style of coding that wires objects to each other via `[[Prototype]]` by calling constructors "works", but it's more complicated and has many more gotchas than just straight object linking via `Object.create()`. Moreover, the way to think about this mechanism is as "behavior delegation", not "inheritance". If you use constructor-style coding, you opt-in to problems with relative polymorphism, confusion/distraction with the mixin pattern short-circuiting the `[[Prototype]]` chain, etc. Objects linked to other objects really is the simpler model. | |
| [Update: I'm incorrect asserting an extra object exists in the prototype chain as a result of this pattern. I correct myself in [this comment](https://gist.github |
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 inherit (child, parent) { | |
| function proxy () {}; | |
| proxy.prototype = parent.prototype; | |
| child.prototype = new proxy(); | |
| }; | |
| function Parent () {} | |
| function Child () {} | |
| inherit(Child, Parent); |
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 Car (licensePlate) { | |
| this.licensePlate = licensePlate; | |
| } | |
| Car.prototype.isLegal = function () { | |
| return !!this.licensePlate; | |
| }; | |
| function Mustang (licensePlate) { | |
| Car.call(this, licensePlate); |
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 makeReadOnlyString (value) { | |
| return function () { | |
| return value; | |
| }; | |
| } | |
| var getTestString = makeReadOnlyString('You can\'t modify me!'); | |
| console.log(getTestString()); | |
| getTestString().replace(/^.*$/, 'Yes I can!'); |
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
| <!DOCTYPE> | |
| <html> | |
| <head> | |
| </head> | |
| <body> | |
| <canvas style="height: 400px; width: 400px; border: solid 1px #000;"></canvas> | |
| <script> | |
| var color = '#f0f'; | |
| var radius = 50; | |
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
| # Taken from http://nodebox.net/code/index.php/shared_2008-02-26-23-26-39 | |
| # Pasting this here because I want to learn from it. | |
| # All credit goes to "Mark M." (See source link) | |
| from nodebox.graphics import BezierPath | |
| class spiral(BezierPath): | |
| '''A logarithmic spiral beginning at start_x, start_y and spiraling toward the point end_x, end_y. | |
| Decay: determines how tightly the spiral turns. Negative values for decay result in the spiral turning | |
| opposite direction. A value of zero results in a line | |
| Length: is how many times around the spiril turns. |
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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script src="http://rekapi.com/vendor/require.js"></script> | |
| </head> | |
| <body> | |
| <div id="canvas" style="background: #eee; overflow: hidden; height: 500px; width: 600px; position: relative;"> | |
| <img src="http://rekapi.com/demo/img/octocat.png" style="display: none; position: absolute; top: 0; left: 0;"> | |
| <img src="https://www.google.com/images/srpr/logo3w.png" style="display: none; position: absolute; top: 0; left: 0;"> | |
| </div> |