Skip to content

Instantly share code, notes, and snippets.

@joshball
Created October 10, 2014 14:54
Show Gist options
  • Save joshball/fc29814cc794c8d18fd4 to your computer and use it in GitHub Desktop.
Save joshball/fc29814cc794c8d18fd4 to your computer and use it in GitHub Desktop.
different.js code
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;
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());
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();
}
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