Posted 21 days ago
0 Comments
Pub/Sub (Dustin Diaz's)
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 };
Posted 1 month ago
0 Comments
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 });
Posted 1 month ago
0 Comments
Playing With node.js
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/');
Posted 1 month ago
0 Comments
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();
Posted 1 month ago
0 Comments
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 };
Posted 1 month ago
0 Comments
My "waitUntil" method
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);
Posted 2 months ago
0 Comments
The Y Combinator (Crockford)
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
Posted 2 months ago
0 Comments