en.javascript.info/1-js/09-classes/02-class-inheritance/3-class-extend-object/task.md
Ilya Kantor 2f725bcd01 minor
2019-09-29 13:46:59 +03:00

820 B

importance: 5


Class extends Object?

As we know, all objects normally inherit from Object.prototype and get access to "generic" object methods like hasOwnProperty etc.

For instance:

class Rabbit {
  constructor(name) {
    this.name = name;
  }
}

let rabbit = new Rabbit("Rab");

*!*
// hasOwnProperty method is from Object.prototype
alert( rabbit.hasOwnProperty('name') ); // true
*/!*

But if we spell it out explicitly like "class Rabbit extends Object", then the result would be different from a simple "class Rabbit"?

What's the difference?

Here's an example of such code (it doesn't work -- why? fix it?):

class Rabbit extends Object {
  constructor(name) {
    this.name = name;
  }
}

let rabbit = new Rabbit("Rab");

alert( rabbit.hasOwnProperty('name') ); // true