An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
July 14, 2021 11:43 am GMT
Original Link: https://dev.to/soorajs98/javascript-question-day-3-3ahk
JavaScript question Day 3
What's the output ?
const shape = { radius: 10, diameter() { return this.radius * 2; }, perimeter: () => 2 * Math.PI * this.radius,};console.log(shape.diameter());console.log(shape.perimeter());
- A:
20
and62.83185307179586
- B:
20
andNaN
- C:
20
and63
- D:
NaN
and63
Answer: B
Note that the value of diameter
is a regular function, whereas the value of perimeter
is an arrow function.
With arrow functions, the this
keyword refers to its current surrounding scope, unlike regular functions! This means that when we call perimeter
, it doesn't refer to the shape object, but to its surrounding scope (window for example).
There is no value radius
on that object, which returns NaN
.
Original Link: https://dev.to/soorajs98/javascript-question-day-3-3ahk
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To