Skip to content

Instantly share code, notes, and snippets.

View jeremyckahn's full-sized avatar

Jeremy Kahn jeremyckahn

View GitHub Profile
@jeremyckahn
jeremyckahn / gist:5649930
Created May 25, 2013 17:25
@varemenos and @jeremyckahn tutoring session notes - 5/25/2013
5/25/2013
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++) {
@jeremyckahn
jeremyckahn / gist:5561999
Created May 12, 2013 01:07
@getify's saved gist comment #2
@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;
@jeremyckahn
jeremyckahn / gist:5561998
Created May 12, 2013 01:06
@getify's saved gist comment #1
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
@jeremyckahn
jeremyckahn / inherit-by-proxy.js
Created May 10, 2013 04:25
Proxy-based inheritance pattern in JavaScript.
function inherit (child, parent) {
function proxy () {};
proxy.prototype = parent.prototype;
child.prototype = new proxy();
};
function Parent () {}
function Child () {}
inherit(Child, Parent);
@jeremyckahn
jeremyckahn / js-inheritance.js
Last active December 17, 2015 01:39
Simple example of inheritance in JavaScript.
function Car (licensePlate) {
this.licensePlate = licensePlate;
}
Car.prototype.isLegal = function () {
return !!this.licensePlate;
};
function Mustang (licensePlate) {
Car.call(this, licensePlate);
@jeremyckahn
jeremyckahn / readonly-string.js
Created May 5, 2013 18:22
Simple example of how to make a read-only string in JavaScript
function makeReadOnlyString (value) {
return function () {
return value;
};
}
var getTestString = makeReadOnlyString('You can\'t modify me!');
console.log(getTestString());
getTestString().replace(/^.*$/, 'Yes I can!');
@jeremyckahn
jeremyckahn / animation.html
Created April 27, 2013 21:17
An animation example from a tutoring session.
<!DOCTYPE>
<html>
<head>
</head>
<body>
<canvas style="height: 400px; width: 400px; border: solid 1px #000;"></canvas>
<script>
var color = '#f0f';
var radius = 50;
@jeremyckahn
jeremyckahn / gist:5202638
Created March 20, 2013 06:03
A logarithmic spiral drawn with Python (copy/pasted)
# 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.
@jeremyckahn
jeremyckahn / two-rekapi-domactors.html
Created February 14, 2013 04:51
Rekapi example using two DOMActors
<!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>