Explain the concept of Prototypal Inheritance in JavaScript.

Instruction: Describe how Prototypal Inheritance works in JavaScript and provide an example.

Context: This question assesses the candidate's understanding of the core principles of JavaScript's object-oriented features and their ability to explain and implement prototypal inheritance.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

JavaScript objects can delegate property lookup through an internal prototype link. Lookup checks the object first, then successive prototypes until a property is found or the chain reaches null. An object created with Object.create(null) has no inherited Object.prototype methods.

const personMethods = { greet() { return `Hello, ${this.name}`; } }; const ada = Object.create(personMethods); ada.name = 'Ada'; console.log(ada.greet()); // Hello, Ada...

Related Questions