Created
July 30, 2018 16:49
-
-
Save kristyburge/3d6040e0c0da06dc93d1d953f0ca803d to your computer and use it in GitHub Desktop.
Using a function and assigning it to an object
This file contains 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 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