What is the output of the following? “`js function Person(name) { this.name = name; } Person.prototype.greet = function() { return `Hi, ${this.name}`; }; const p = Object.create(Person.prototype); p.name = ‘Alice’; console.log(p.greet()); console.log(p instanceof Person); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js function Person(name) { this.name = name; } Person.prototype.greet = function() { return `Hi, ${this.name}`; }; const p = Object.create(Person.prototype); p.name = ‘Alice’; console.log(p.greet()); console.log(p instanceof Person); “`

Key points

  • Object.create() creates an object with a specified prototype
  • Prototype chain allows 'p' to access 'greet' method
  • 'p' is an instance of 'Person' due to prototype inheritance

Ready to go further?

Related questions