Suggest an editImprove this articleRefine the answer for “What does Object.create() do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Object.create()`** is one of the basic methods for working with prototypes in JavaScript, letting you create a new object by manually specifying its prototype (what it inherits from). **Key point:** `Object.create(null)` creates an object with no prototype, which does not inherit even `toString` or `hasOwnProperty`.Shown above the full answer for quick recall.Answer (EN)Image`Object.create()` is one of the **basic methods for working with prototypes in JavaScript**. It lets you **create a new object by manually specifying its prototype** (what it inherits from). --- ## In short Syntax: ```javascript Object.create(proto, [propertiesObject]) ``` | Parameter | Description | |---|---| | `proto` | The object that becomes the **prototype of the new object** (or `null`) | | `propertiesObject` *(optional)* | Extra properties for the new object (in descriptor format) | --- ## Example 1 - basic usage ```javascript const user = { sayHi() { console.log(`Hello, ${this.name}!`); } }; const tim = Object.create(user); // create an object that inherits from user tim.name = 'Tim'; tim.sayHi(); // "Hello, Tim!" ``` Here: - `user` is the **prototype** (`[[Prototype]]`) for `tim`; - `tim` has no own `sayHi` method, but it is **inherited via the prototype**. --- ## Example 2 - checking the prototype ```javascript console.log(Object.getPrototypeOf(tim) === user); // true ``` `Object.create()` literally creates **a new object whose** `__proto__` **equals** `user`. --- ## Example 3 - an object with no prototype ```javascript const data = Object.create(null); data.a = 1; console.log(data); // { a: 1 } console.log(Object.getPrototypeOf(data)); // null console.log(data.toString); // undefined ``` Such an object **inherits nothing**, not even `toString`, `hasOwnProperty`, etc. It is used for **plain dictionaries (map objects)** with no name conflicts. --- ## Example 4 - with `propertiesObject` (the second argument) You can set properties with configuration right away, as in `Object.defineProperty()`: ```javascript const person = Object.create({}, { name: { value: 'Tim', writable: false, enumerable: true }, age: { value: 25, writable: true } }); console.log(person.name); // "Tim" person.name = 'Oleh'; // will not change console.log(person.name); // "Tim" ``` The second parameter lets you **describe property attributes in detail** (`value`, `writable`, `enumerable`, `configurable`). --- ## Example 5 - manual "inheritance" ```javascript const Animal = { eat() { console.log('I eat'); } }; const Dog = Object.create(Animal); Dog.bark = function() { console.log('Woof!'); }; Dog.eat(); // "I eat" - inherited from Animal Dog.bark(); // "Woof!" - own ``` A great way to implement simple **prototypal inheritance** without classes and constructors. --- ## Example 6 - an alternative to `class` ```javascript const UserProto = { init(name) { this.name = name; return this; }, greet() { console.log(`Hello, ${this.name}`); } }; const user = Object.create(UserProto).init('Tim'); user.greet(); // "Hello, Tim" ``` This approach is often used in a **functional OOP style** - no classes, but inheritance via the prototype. --- ## Summary | What it does | Example | Result | |---|---|---| | Creates an object with the given prototype | `Object.create(proto)` | A new object whose `[[Prototype]] = proto` | | Creates an object with no prototype | `Object.create(null)` | A "clean" object with no `toString`, `hasOwnProperty` | | Properties with attributes can be set | `Object.create({}, { key: { value: 1 } })` | Equivalent to `defineProperty()` | | Used for inheritance | `const child = Object.create(parent)` | `child` inherits `parent`'s properties |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.