Last active
August 29, 2015 14:16
-
-
Save gregtatum/d551256d8e1bbc0ed639 to your computer and use it in GitHub Desktop.
State and config in a function
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 characters
| function logger( state, msg ) { | |
| state.count++ | |
| console.log( state.count, this.msg ) | |
| } | |
| var log = () => logger( {count:0}, "Calling from an arrow function" ) | |
| log() | |
| log() | |
| log() |
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 characters
| function logger() { | |
| this.count++ | |
| console.log( this.count, this.msg ) | |
| } | |
| var log = logger.bind({ | |
| count: 0, | |
| msg: "Logging from a bound function" | |
| }) | |
| log() | |
| log() | |
| log() |
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 characters
| function createLogger( msg ) { | |
| var count = 0 | |
| return function() { | |
| count++ | |
| console.log( count, msg ) | |
| } | |
| } | |
| var log = createLogger( "Trapping variables in a closure" ) | |
| log() | |
| log() | |
| log() |
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 characters
| function Logger( msg ) { | |
| this.msg = msg | |
| this.count = 0 | |
| } | |
| Logger.prototype.log = function() { | |
| this.count++ | |
| console.log( this.count, this.msg ) | |
| } | |
| var logger = new Logger("A typical Object Oriented logger") | |
| logger.log() | |
| logger.log() | |
| logger.log() |
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 characters
| function logger( state, msg ) { | |
| state.count++ | |
| console.log( state.count, this.msg ) | |
| } | |
| var log = _.partial( logger, {count:0}, "Calling from a partially applied function" ) | |
| log() | |
| log() | |
| log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment