Last active
August 29, 2015 14:07
-
-
Save AlexArchive/eece353cbd93e2c50b3c to your computer and use it in GitHub Desktop.
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
/* | |
Instead of repeating cross-cutting concerns in every function, we | |
can use the "doWork" function as an abstraction. | |
*/ | |
var work = function(){ | |
console.log("working hard"); | |
}; | |
var doWork = function(func){ | |
console.log("starting"); | |
try { | |
func(); | |
} | |
catch(ex){ | |
console.log(ex); | |
} | |
console.log("ending"); | |
}; | |
doWork(work); |
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
// Immediately invoked function expression or IFFE. | |
(function() { | |
var createFunc = function() { | |
return function () { | |
console.log("hello."); | |
} | |
} | |
var func = createFunc(); | |
func(); | |
})(); |
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
// Revealing module pattern. | |
var createWorker = function(){ | |
// Encapsulate code inside of a function/module. | |
var job1 = function() { | |
console.log("job1"); | |
}; | |
var job2 = function() { | |
console.log("job2"); | |
}; | |
return { | |
job1: job1, | |
job2: job2 | |
} | |
}; | |
var worker = createWorker(); | |
worker.job1(); | |
worker.job2(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment