Created
August 30, 2012 20:27
-
-
Save qmmr/3540038 to your computer and use it in GitHub Desktop.
JavaScript: Objects, Inheritance, Privacy and Stuff
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 fairy (luckyNumber) { | |
return { | |
toString: function () { | |
return "The number you're looking for is " + luckyNumber; | |
} | |
}; | |
} | |
function wizard (luckyNumber) { | |
var that = fairy (luckyNumber); | |
that.test = function (myNumber) { | |
return myNumber === luckyNumber; | |
}; | |
return that; | |
} | |
// shared secrets | |
function gizmo (id, secret) { | |
secret = secret || {}; | |
secret.id = id; | |
return { | |
toString: function () { | |
return "gizmo " + secret.id; | |
}; | |
}; | |
} | |
function hoozit (id) { | |
var secret = {}; /* final */ | |
var that = gizmo(id, secret); | |
that.test = function (testId) { | |
return testId === secret.id; | |
}; | |
return that; | |
// super methods |
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 memoizer(memo, formula) { | |
var recur = function (n) { | |
var result = memo[n]; | |
if (typeof result !== 'number') { | |
result = formula(recur, n); | |
memo[n] = result; | |
} | |
return result; | |
}; | |
return recur; | |
} | |
var factorial = memoizer([1, 1], function (recur, n) { | |
return n * recur(n - 1); | |
}); | |
var fibonacci = memoizer([0, 1], function (recur, n) { | |
return recur(n - 1) + recur(n - 2); | |
}); |
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 () { | |
setTimeout(function recurring () { | |
// do something and call ajax | |
$('#info').load('something.php', function () { | |
setTimeout(recurring, 1000); | |
}); | |
}; | |
})(); | |
$(function () { | |
var ul = $('ul'); | |
var i = 0; | |
// pass named function into setTimeout so that it can be called again | |
// when ajax call is complete | |
setTimeout(function getDate() { | |
var started = new Date(); | |
var i = index; | |
index++; | |
$.get('/home/date', function (date) { | |
var end = new Date(); | |
ul.append('<li>Request #' + i + ' started at ' + started.getHours() + ':' + started.getMinutes() + ' and ended ' + end.getHours() + ':' + end.getMinutes() + '</li>'); | |
// ajax is complete so we call the setTimeout again | |
setTimeout(getDate, 2000); | |
} | |
}, 2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment