Skip to content

Instantly share code, notes, and snippets.

@kristyburge
Created July 30, 2018 16:49
Show Gist options
  • Save kristyburge/3d6040e0c0da06dc93d1d953f0ca803d to your computer and use it in GitHub Desktop.
Save kristyburge/3d6040e0c0da06dc93d1d953f0ca803d to your computer and use it in GitHub Desktop.
Using a function and assigning it to an object
var dog = {
breed: 'Beagles',
lovesToChase: 'rabbits'
};
function chase() {
console.log(this.breed + ' loves chasing ' + this.lovesToChase + '.');
// console.log(this);
}
dog.foo = chase;
dog.foo(); // returns Beagles loves chasing rabbits.
chase(); // returns undefined because when run in the global context, window object does not have these properties defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment