Last active
August 29, 2015 14:01
-
-
Save dtothefp/2a1cf2f6983ba84c5d8e to your computer and use it in GitHub Desktop.
private methods and vars
This file contains 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
//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