Last active
April 5, 2016 09:01
-
-
Save jneen/f6d4ddce022fa51bab745de067023ccc to your computer and use it in GitHub Desktop.
Revisions
-
Jeanine Adkisson revised this gist
Apr 5, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ var Dynamic = function() { var context = {} var defaults = {} @@ -63,4 +63,4 @@ function Dynamic() { setDefault: setDefault, remember: remember } }(); -
Jeanine Adkisson revised this gist
Apr 5, 2016 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -16,7 +16,12 @@ function Dynamic() { } function get(name) { if (typeof context[name] === 'undefined') { return defaults[name]; } else { return context[name]; } } function setDefault(name, val) { -
Jeanine Adkisson created this gist
Apr 5, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ function Dynamic() { var context = {} var defaults = {} function bind(name, val, fn) { try { var save = context[name]; context[name] = val; var out = fn(); } finally { context[name] = save; } return out; } function get(name) { return context[name]; } function setDefault(name, val) { if (arguments.length > 1) defaults[name] = val; return defaults[name]; } function contextCopy() { var out = {} for (var k in context) { if (!hasOwnProperty.call(context, k)) continue; out[k] = context[k]; } return out; } function remember(fn) { var savedContext = contextCopy(); return function() { try { var swap = context; context = savedContext; var out = fn.apply(null, arguments); } finally { context = swap; } return out; }; } return { bind: bind, get: get, setDefault: setDefault, remember: remember } }