Skip to content

Instantly share code, notes, and snippets.

@nephilim
Last active December 13, 2015 18:09
Show Gist options
  • Save nephilim/4953315 to your computer and use it in GitHub Desktop.
Save nephilim/4953315 to your computer and use it in GitHub Desktop.
Created to demonstrate one of possible problems with a sample code in chapter four of the book Javascript Patterns. For more information, refer to an example on page 92 of the book.
var func = function() {
if ( !func.cache ) {
console.log("cache initialized @" + new Date());
func.cache = "complex result";
}
return func.cache;
};
var func2 = func;
func2();
// cache initialized
// "complex result"
func2();
// "complex result"
func();
// "complex result"
func = function() {
console.log("another");
};
func();
func2();
//cache initialized
//"complex result"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment