const shape = {
radius: 10,
diameter() {
return this.radius * 2;
},
perimeter: () => 2 * Math.PI * this.radius,
};
console.log(shape.diameter());
console.log(shape.perimeter());
bio.link/angulardev
const shape = {
radius: 10,
diameter() {
return this.radius * 2;
},
perimeter: () => 2 * Math.PI * this.radius,
};
console.log(shape.diameter());
console.log(shape.perimeter());
bio.link/angulardev
Add a console.log(this)
to each of your methods and see if you notice a difference. This is intentional. Arrow function methods do not bind to the this
of their object. That’s why you should not use them if you need to reference a property in the object.
I’m sure if you google for “arrow functions and this” you will get a much better explanation than I have time to give here