← Back to JavaScript Course | Chapter 7: OOP & Prototypes | Lesson 3 of 6

JS Object.create

Object.create makes a new object that uses another object as its parent, like starting a new club that follows an older club's rules. You choose the parent yourself.
Syntax
javascript
const newObject = Object.create(prototypeObject);
const newObject = Object.create(prototypeObject, descriptors);

Creating an Object

Object.create creates a new object with the prototype you provide. This is a more direct way to set up inheritance than using a constructor function, since you specify the exact prototype object up front.

उदाहरण: Creating an Object

javascript
// Declare the constant `proto`, set to `{ greet() { return "hi"; } }`
// Declare the constant `proto`, set to `{ greet() { return "hi"; } }`
const proto = { greet() { return "hi"; } };
// Declare the constant `obj`, set to `Object.create(proto)`
// Declare the constant `obj`, set to `Object.create(proto)`
const obj = Object.create(proto);
// Print `obj.greet()` to the console
// Print `obj.greet()` to the console
console.log(obj.greet());

Using null as Prototype

Object.create(null) makes an object without Object.prototype in its prototype chain.

This is useful for building a genuinely empty object with no inherited methods at all — safer than a plain {} when you want to avoid accidental prototype pollution or collisions.

उदाहरण: Using null as Prototype

javascript
const obj = Object.create(null);
obj.name = "Sam";
console.log(obj.name);
console.log(obj.toString); // undefined, no inherited methods

Adding Properties

The second argument can define properties while the object is created. Using a descriptor object lets you also mark a property as non-writable or non-enumerable at the moment of creation, not just set its initial value.

उदाहरण: Adding Properties

javascript
const obj = Object.create({}, {
  name: { value: "Sam", writable: true },
});
// Print `obj.name` to the console
// Print `obj.name` to the console
console.log(obj.name);

Property Descriptors

Property descriptors control whether a property can be changed, shown in loops, or removed. Descriptors give fine-grained control that plain assignment doesn't offer, like making a property read-only or invisible to for...in loops and Object.keys().

उदाहरण: Property Descriptors

javascript
const obj = {};
Object.defineProperty(obj, "id", { value: 1, writable: false, enumerable: false });
obj.id = 99; // silently fails, read-only
console.log(obj.id);
console.log(Object.keys(obj)); // [], hidden from enumeration

Object.create in Practice

Object.create is useful when you want shared behavior or a custom prototype. It's commonly reached for when you want an object that behaves like a specific prototype's instance without going through a constructor function or class at all.

उदाहरण: Object.create in Practice

javascript
// Declare the constant `carPrototype`, set to `{ drive() { console.log("Driving"); } }`
// Declare the constant `carPrototype`, set to `{ drive() { console.log("Driving"); } }`
const carPrototype = { drive() { console.log("Driving"); } };
// Declare the constant `myCar`, set to `Object.create(carPrototype)`
// Declare the constant `myCar`, set to `Object.create(carPrototype)`
const myCar = Object.create(carPrototype);
// Call `myCar.drive()`
// Call `myCar.drive()`
myCar.drive();
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.