+1 vote
in JavaScript by

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

1 Answer

0 votes
by

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 or called upon class instances. Since freddie is an instance of class Chameleon, the function cannot be called upon it. A TypeError is thrown.

Related questions

0 votes
asked Mar 13 in JavaScript by DavidAnderson
+1 vote
asked Feb 24 in JavaScript by DavidAnderson
...