Skip to content

Instantly share code, notes, and snippets.

@henryspivey
Forked from prof3ssorSt3v3/13-this-func.js
Last active October 30, 2019 21:41
Show Gist options
  • Save henryspivey/2383fffcbc02eb4f5fb23eb62fd5fbb7 to your computer and use it in GitHub Desktop.
Save henryspivey/2383fffcbc02eb4f5fb23eb62fd5fbb7 to your computer and use it in GitHub Desktop.
// Using `this` inside an object
// The object has functions created three different ways
// What will be the result of the three log statements?
const circle = {
radius: 10,
circumference: function () {
return (2 * Math.PI * this.radius);
},
diameter() {
return (this.radius * 2);
},
area: () => {
return (Math.PI * Math.PI * this.radius);
}
}
console.log(circle.area()); // NaN because arrow () uses lexical, in this case it's global, scope.
console.log(circle.diameter()); // 20
console.log(circle.circumference()); //20π
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment