Skip to content

Instantly share code, notes, and snippets.

@mikintosh
Created October 21, 2016 10:23
Show Gist options
  • Save mikintosh/4befef9f223b93c6629898b73decc418 to your computer and use it in GitHub Desktop.
Save mikintosh/4befef9f223b93c6629898b73decc418 to your computer and use it in GitHub Desktop.
Returning an object with functions (CodeCombat)
function summonHero (name, position) {
return {
say: function (sentence) {
alert(sentence);
},
move: function (position) {
console.log('Moving to x=' + position.x + ' and y=' +position.y);
this.pos = position;
},
name: name,
pos: position
};
}
var ana = summonHero('Ana', {x:0,y:0});
console.log(ana.say('hello'));
console.log(ana.pos);
console.log(ana.move({x:10,y:10}));
console.log(ana.pos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment