shawndumas http://blog.shawndumas.com javascript posterous.com Thu, 08 Jul 2010 13:02:00 -0700 My Invoke Method http://blog.shawndumas.com/invoke-method http://blog.shawndumas.com/invoke-method

See Prototype’s invoke.

Head over to remy sharp’s JS Bin and play with mine.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Sat, 12 Jun 2010 07:29:00 -0700 Pub/Sub (Dustin Diaz's) http://blog.shawndumas.com/pubsub-dustin-diazs http://blog.shawndumas.com/pubsub-dustin-diazs

See Diaz’s article.

Head over to remy sharp’s JS Bin and play.

 1 function Publisher(name) {
 2     this.name = name;
 3     this.subscribers = [];
 4 }
 5 Publisher.prototype.deliver = function (data, scope) {
 6     scope = scope || window;
 7 
 8     var self = this;
 9 
10     this.subscribers.forEach(
11         function (value) {
12             value.call(scope, self.name, data);
13         }
14     );
15 
16     return this;
17 };
18 Function.prototype.subscribe = function (publisher) {
19     var self = this,
20         isExistent = publisher.subscribers.some(
21             function (value) {
22                 return (value === self);
23             }
24         );
25 
26     if (!isExistent) {
27         publisher.subscribers.push(this);
28     }
29 
30     return this;
31 };
32 Function.prototype.unsubscribe = function (publisher) {
33     var self = this;
34 
35     publisher.subscribers = publisher.subscribers.filter(
36         function(value) {
37             if (value !== self) {
38                 return value;
39             }
40         }
41     );
42 
43     return this;
44 };

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Wed, 09 Jun 2010 08:07:00 -0700 More FizzBuzz http://blog.shawndumas.com/more-fizzbuzz http://blog.shawndumas.com/more-fizzbuzz

A functional version (won’t work in IE).

Head over to remy sharp’s JS Bin and play.

See my original FizzBuzz.

 1 (function (n) {
 2     var r = [];
 3 
 4     while (n--) {
 5         r.push(n + 1);
 6     }
 7 
 8     return r.reverse();
 9 })(100).map(function (n) {
10     return !(n % 15) ?
11         'FizzBuzz' :
12         !(n % 3) ?
13             'Fizz' :
14             !(n % 5) ?
15                 'Buzz' :
16                 n;
17 });

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 07 Jun 2010 20:18:00 -0700 Playing With node.js http://blog.shawndumas.com/playing-with-nodejs http://blog.shawndumas.com/playing-with-nodejs

Streaming a PNG in node.js

 1 var sys = require('sys'),
 2     http = require('http'),
 3 url = require('url');
 4 
 5 http.createServer(function (request, response) {
 6     var f = (url.parse(request.url, true)).query.file
 7         fs = require('fs'),
 8         path = './' + f,
 9         rs = fs.createReadStream(path);
10 
11     response.writeHead(200, { 'Content-Type': 'image/png' });
12     rs.addListener('data', function (b) {
13         response.write(b, 'binary');
14     });
15     rs.addListener('end', function () { response.end(); })
16 }).listen(8000);
17 
18 sys.puts('Server running at http://127.0.0.1:8000/');

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Sun, 06 Jun 2010 18:05:00 -0700 My FizzBuzz http://blog.shawndumas.com/my-fizzbuzz http://blog.shawndumas.com/my-fizzbuzz

See Jeff Atwood’s article.

Head over to remy sharp’s JS Bin and play.

 1 var fizzBuzz = function (n) {
 2   n = n || 100;
 3 
 4   var i = 0,
 5       r = [];
 6 
 7   while (i++ < n) {
 8     r.push(
 9       (!(i % 3) &&  (i % 5)) && ('Fizz (' + i + ')')     ||
10       ( (i % 3) && !(i % 5)) && ('Buzz (' + i + ')')     ||
11       (!(i % 3) && !(i % 5)) && ('FizzBuzz (' + i + ')') ||
12       i
13     );
14   }
15 
16   return r.join('<br />');
17 };
18 
19 document.getElementById('result').innerHTML = fizzBuzz();

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 31 May 2010 04:39:00 -0700 Async Method Queue Chaining (Dustin Diaz) http://blog.shawndumas.com/async-method-queue-chaining-dustin-diaz http://blog.shawndumas.com/async-method-queue-chaining-dustin-diaz

See Diaz’s article.

Head over to remy sharp’s JS Bin and play.

 1 var Queue = function () {
 2   if (!(this instanceof arguments.callee)) {
 3       return new arguments.callee();
 4   }
 5   this._methods = [];
 6   this._response = null;
 7   this._flushed = false;
 8 }
 9 
10 Queue.prototype = {
11   add: function(fn) {
12     (this._flushed) ?
13       fn(this._response) :
14       this._methods.push(fn);
15   },
16   flush: function(resp) {
17     (this._flushed) && (return);
18     this._response = resp;
19     this._flushed = true;
20     while (this._methods[0]) {
21       this._methods.shift()(resp);
22     }
23   }
24 };

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Sun, 30 May 2010 05:06:00 -0700 My "waitUntil" method http://blog.shawndumas.com/click-here-to-set-a-title-846 http://blog.shawndumas.com/click-here-to-set-a-title-846

Head over to remy sharp’s JS Bin and play.

 1 Function.prototype.waitUntil = function (condition, interval) {
 2     interval = interval || 100;
 3 
 4     var fn = this,
 5         shell = function () {
 6             var timer = setInterval(
 7                 function () {
 8                     var check;
 9 
10                     try {
11                         check = !!(condition());
12                     } catch (e) {
13                         check = false;
14                     }
15 
16                     if (check) {
17                         clearInterval(timer);
18                         delete timer;
19                         fn();
20                     }
21                 },
22                 interval
23             );
24         };
25 
26     return shell;
27 };
28 
29 var done = false;
30 
31 (function () {
32     document.getElementById('result').innerHTML = 'I waited...';
33 }).waitUntil(function () { return !!(done); })();
34 
35 setTimeout(function () { done = true; }, 2000);

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Wed, 26 May 2010 19:37:00 -0700 The Y Combinator (Crockford) http://blog.shawndumas.com/click-here-to-set-a-title-289 http://blog.shawndumas.com/click-here-to-set-a-title-289

See Crockford’s article.

Head over to remy sharp’s JS Bin and play.

 1 var Y = function (le) {
 2   return ((function (f) {
 3     return f(f);
 4   })(function (f) {
 5     return le(function (x) {
 6       return f(f)(x);
 7     });
 8   }));
 9 };
10 
11 var factorial = Y(function (fac) {
12   return function (n) {
13     return (n <= 2) ? n : n * fac(n - 1);
14   };
15 });
16 
17 !!(factorial(5) === 120); //true

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/573980/me.jpg http://posterous.com/people/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas