Created
October 10, 2014 14:54
-
-
Save joshball/fc29814cc794c8d18fd4 to your computer and use it in GitHub Desktop.
different.js code
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 createPerson(name){ | |
console.log('\ncreatePerson.closure: %s', name); | |
var cakesEaten = 0; | |
return { | |
eatCake: function(){ | |
console.log('%s: om nom nom nom', name); | |
cakesEaten++; | |
}, | |
stillHungry: function(){ | |
console.log('more cake %s?', name) | |
if(cakesEaten < 3){ | |
console.log('\t%s: Yes please! I have only had %d cakes!', name, cakesEaten); | |
return true; | |
} | |
console.log('\t%s: No thank you! I have eaten %d cakes!', name, cakesEaten); | |
return false; | |
} | |
}; | |
}; | |
module.exports = createPerson; |
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
var cakes = ['coffee','cheese','bundt','angel'] | |
var Person = require('./Person.this'); | |
var person = new Person('Abby'); | |
cakes.forEach(person.eatCake); | |
console.log('person.stillHungry:', person.stillHungry()); | |
var createPerson = require('./createPerson.closure'); | |
var person = createPerson('Bill'); | |
cakes.forEach(person.eatCake); | |
console.log('person.stillHungry:', person.stillHungry()); |
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
var Person = require('./Person.this'); | |
var person = new Person('Abby'); | |
while(person.stillHungry()){ | |
person.eatCake(); | |
} | |
var createPerson = require('./createPerson.closure'); | |
var person = createPerson('Bill'); | |
while(person.stillHungry()){ | |
person.eatCake(); | |
} |
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 Person(name){ | |
console.log('\nPerson.this: %s', name); | |
this.name = name; | |
this.cakesEaten = 0; | |
}; | |
Person.prototype.eatCake = function(){ | |
console.log('%s: om nom nom nom', this.name); | |
this.cakesEaten++; | |
}; | |
Person.prototype.stillHungry = function(){ | |
console.log('more cake %s?', this.name) | |
if(this.cakesEaten < 3){ | |
console.log('\t%s: Yes please! I have only had %d cakes!', this.name, this.cakesEaten); | |
return true; | |
} | |
console.log('\t%s: No thank you! I have eaten %d cakes!', this.name, this.cakesEaten); | |
return false; | |
}; | |
module.exports = Person; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment