JavaScript question #Day 6

What’s the output ?

class Chameleon {
static colorChange(newColor) {
this.newColor = newColor;
return this.newColor;
}

constructor({ newColor = ‘green’ } = {}) {
this.newColor = newColor;
}
}

const freddie = new Chameleon({ new…

What’s the output ?

class Chameleon {
  static colorChange(newColor) {
    this.newColor = newColor;
    return this.newColor;
  }

  constructor({ newColor = 'green' } = {}) {
    this.newColor = newColor;
  }
}

const freddie = new Chameleon({ newColor: 'purple' });
console.log(freddie.colorChange('orange'));
  • A: orange
  • B: purple
  • C: green
  • D: TypeError

Answer: D

The colorChange function is static. Static methods are designed to live only on the constructor in which they are created, and cannot be passed down to any children. Since freddie is a child, the function is not passed down, and not available on the freddie instance: a TypeError is thrown.


Print Share Comment Cite Upload Translate
APA
Sooraj S | Sciencx (2024-03-29T10:45:26+00:00) » JavaScript question #Day 6. Retrieved from https://www.scien.cx/2021/07/15/javascript-question-day-6/.
MLA
" » JavaScript question #Day 6." Sooraj S | Sciencx - Thursday July 15, 2021, https://www.scien.cx/2021/07/15/javascript-question-day-6/
HARVARD
Sooraj S | Sciencx Thursday July 15, 2021 » JavaScript question #Day 6., viewed 2024-03-29T10:45:26+00:00,<https://www.scien.cx/2021/07/15/javascript-question-day-6/>
VANCOUVER
Sooraj S | Sciencx - » JavaScript question #Day 6. [Internet]. [Accessed 2024-03-29T10:45:26+00:00]. Available from: https://www.scien.cx/2021/07/15/javascript-question-day-6/
CHICAGO
" » JavaScript question #Day 6." Sooraj S | Sciencx - Accessed 2024-03-29T10:45:26+00:00. https://www.scien.cx/2021/07/15/javascript-question-day-6/
IEEE
" » JavaScript question #Day 6." Sooraj S | Sciencx [Online]. Available: https://www.scien.cx/2021/07/15/javascript-question-day-6/. [Accessed: 2024-03-29T10:45:26+00:00]
rf:citation
» JavaScript question #Day 6 | Sooraj S | Sciencx | https://www.scien.cx/2021/07/15/javascript-question-day-6/ | 2024-03-29T10:45:26+00:00
https://github.com/addpipe/simple-recorderjs-demo