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…


This content originally appeared on DEV Community and was authored by Sooraj S

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.


This content originally appeared on DEV Community and was authored by Sooraj S


Print Share Comment Cite Upload Translate Updates
APA

Sooraj S | Sciencx (2021-07-15T07:49:17+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 ,<https://www.scien.cx/2021/07/15/javascript-question-day-6/>
VANCOUVER
Sooraj S | Sciencx - » JavaScript question #Day 6. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/15/javascript-question-day-6/
CHICAGO
" » JavaScript question #Day 6." Sooraj S | Sciencx - Accessed . 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: ]
rf:citation
» JavaScript question #Day 6 | Sooraj S | Sciencx | https://www.scien.cx/2021/07/15/javascript-question-day-6/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.