Object.create lets you make a new object that uses another object as its base. This helps you share properties and methods easily.
Object.create usage in Javascript
const newObject = Object.create(prototypeObject, propertiesObject);
prototypeObject is the object you want to use as the base (prototype).
propertiesObject is optional and lets you add new properties with descriptors.
dog that uses animal as its base. Calling dog.speak() runs the method from animal.const animal = { speak() { console.log('Animal sound'); } }; const dog = Object.create(animal); dog.speak();
john with person as prototype and adds a name property.const person = { greet() { console.log(`Hello, my name is ${this.name}`); } }; const john = Object.create(person, { name: { value: 'John', writable: true, enumerable: true, configurable: true } }); john.greet();
This program creates a car object that inherits the start method from vehicle. Then it adds a new property wheels to car.
const vehicle = { start() { console.log('Vehicle started'); } }; const car = Object.create(vehicle); car.start(); car.wheels = 4; console.log(`Car has ${car.wheels} wheels`);
Objects created with Object.create have their prototype set to the object passed as the first argument.
If you pass null as the prototype, the new object will not inherit anything.
Using Object.create is a simple way to do inheritance without classes.
Object.create makes a new object using another object as a base.
It helps share properties and methods without copying them.
You can add new properties while creating the object.
Practice
Object.create(proto) do in JavaScript?Solution
Step 1: Understand Object.create purpose
Object.create(proto)creates a new object that usesprotoas its prototype, meaning it inherits properties and methods fromproto.Step 2: Differentiate from copying
It does not copy properties but links the new object toprotovia the prototype chain.Final Answer:
Creates a new object withprotoas its prototype. -> Option BQuick Check:
Object.create(proto) sets prototype = proto [OK]
- Thinking it copies properties instead of setting prototype
- Confusing with Object.assign or spread operator
- Assuming it creates arrays or deletes objects
child with prototype parent using Object.create?Solution
Step 1: Recall Object.create syntax
The correct syntax isObject.create(proto)which returns a new object withprotoas prototype.Step 2: Check each option
const child = Object.create(parent); uses correct syntax. const child = Object.create = parent; wrongly assigns. const child = new Object.create(parent); wrongly usesnew. const child = Object.create(parent)(); tries to call the result as a function.Final Answer:
const child = Object.create(parent); -> Option CQuick Check:
Correct syntax = Object.create(proto) [OK]
- Using new with Object.create
- Assigning Object.create to a variable incorrectly
- Calling the result as a function
const parent = { greet() { return 'Hello'; } };
const child = Object.create(parent);
console.log(child.greet());Solution
Step 1: Understand prototype inheritance
childis created withparentas prototype, so it inheritsgreetmethod.Step 2: Calling child.greet()
Sincegreetexists onparent,child.greet()calls that method and returns 'Hello'.Final Answer:
'Hello' -> Option AQuick Check:
Inherited method call returns 'Hello' [OK]
- Expecting undefined because child has no own greet
- Thinking it causes error without own method
- Confusing prototype chain lookup
const proto = { x: 10 };
const obj = Object.create();
console.log(obj.x);Solution
Step 1: Check Object.create usage
Object.create()without arguments is invalid because it requires a prototype object or null.Step 2: Understand consequence
Since no prototype is given, this causes a TypeError or unexpected behavior.Final Answer:
Object.create() requires a prototype argument. -> Option AQuick Check:
Object.create(proto) needs proto argument [OK]
- Calling Object.create without arguments
- Expecting default prototype if none given
- Ignoring TypeError from missing argument
child with prototype parent and add a new property age with value 25 during creation using Object.create?Solution
Step 1: Understand Object.create second argument
The second argument toObject.createis a property descriptor object where each property is defined with descriptors likevalue,writable,enumerable, andconfigurable.Step 2: Check each option
const child = Object.create(parent, { age: { value: 25, writable: true, enumerable: true, configurable: true } }); correctly uses property descriptor forage. const child = Object.create(parent); child.age = 25; adds property after creation, not during. Options A and C use incorrect descriptor syntax.Final Answer:
const child = Object.create(parent, { age: { value: 25, writable: true, enumerable: true, configurable: true } }); -> Option DQuick Check:
Use property descriptors in second argument [OK]
- Adding properties directly in second argument without descriptors
- Adding properties after creation instead of during
- Using wrong syntax for property descriptors
Ready to prove it?
Pass the test and this pattern moves unknown
