Skip to content

Instantly share code, notes, and snippets.

@dtothefp
Last active August 29, 2015 14:01
Show Gist options
  • Save dtothefp/2a1cf2f6983ba84c5d8e to your computer and use it in GitHub Desktop.
Save dtothefp/2a1cf2f6983ba84c5d8e to your computer and use it in GitHub Desktop.
private methods and vars
//http://www.crockford.com/javascript/little.html
var singleton = (function(){
var privateHello = "hello";
function privateFunction(something){
privateHello = privateHello + " " + something;
}
return {
changeSomething: function(a){
privateFunction(a);
},
logSomething: function(){
console.log(privateHello);
}
}
}());
function gizmo(id, secret){
secret = secret || {};
secret.id = id;
return {
toString: function() {
return "gizmo" + secret.id;
}
}
}
function hoozit(id){
var secret = {};
var that = gizmo(id, secret);
that.test = function(testId){
return testId === secret.id;
};
return that;
}
var 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 fibSeq = memoizer([1,1], function(recur, n) {
return n * recur(n - 1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment