Codectionary / Developer documentation / JavaScript

Prototype Delegation and Object.create

JavaScript property lookup can delegate to an object’s prototype when an own property is missing. Object.create builds an object with a chosen prototype. This explains inherited methods and shared behavior without implying that inherited data is copied into each object.

Syntax

const instance = Object.create(prototype);
Object.getPrototypeOf(instance);

Examples

Prototype Delegation and Object.create: working example

Run this standalone example in a modern browser console unless another environment is specified. Expected result: Logs are Task: Review, false, and true.

const shared = {
  describe() { return `Task: ${this.title}`; }
};
const task = Object.create(shared);
task.title = "Review";
console.log(task.describe());
console.log(Object.hasOwn(task, "describe"));
console.log(Object.getPrototypeOf(task) === shared);
  1. Expected result: Logs are Task: Review, false, and true.
  2. describe is found on shared, but this is the object used at the call site.
  3. Only title is assigned directly to task in this example.

Best practices

  • Avoid mutating shared prototypes during normal application work.
  • Use Object.hasOwn when enumerating only a record’s own fields.

Runtime & versions

Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ECMAScript specification

Specifications & further reading