Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save esova-ana/6514664630fc966dcee82a5435083bd6 to your computer and use it in GitHub Desktop.
Save esova-ana/6514664630fc966dcee82a5435083bd6 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