Permalink
Browse files

Pass gyre into Falcon constructor.

... instead of referening the var defined in the outer closure.

Thanks to "shiftins" (http://news.ycombinator.com/item?id=4763479) for
the recommendation.
  • Loading branch information...
1 parent d8f20bc commit 207480cae2823dd57a73062c933295cfdd0a8631 @jsvine committed Nov 9, 2012
Showing with 6 additions and 5 deletions.
  1. +6 −5 txtbirds.js
View
@@ -1,8 +1,8 @@
(function () {
var root = this;
- var gyre = root.document.getElementById("gyre");
+ var g = root.document.getElementById("gyre");
- var Falcon = function () {
+ var Falcon = function (gyre) {
this.flaps = [ '------', '/‾\\/‾\\', '‾‾\\/‾‾' ];
this.flap_i = 0;
this.flap_rate = 230;
@@ -11,6 +11,7 @@
this.veer_coefficient_horizontal = 50;
this.lifespan = 15 * 1000;
this.timers = [];
+ this.gyre = gyre;
};
Falcon.prototype = {
@@ -21,7 +22,7 @@
[ "top", "left" ].forEach(function (s) {
_this.el.style[s] = (50 + (root.Math.random() < 0.5 ? -1 : 1) * root.Math.random() * 80) + "%";
});
- gyre.appendChild(this.el);
+ this.gyre.appendChild(this.el);
return this;
},
flap: function () {
@@ -41,13 +42,13 @@
},
vanish: function () {
this.timers.forEach(function (t) { root.clearInterval(t); });
- gyre.removeChild(this.el);
+ this.gyre.removeChild(this.el);
return this;
}
};
var launch = function () {
- return (new Falcon()).hatch().fly();
+ return (new Falcon(g)).hatch().fly();
};
root.setInterval(launch, 730);

0 comments on commit 207480c

Please sign in to comment.