JS Built-ins
ObjectObject.createbeginner

Object.create() β€” create with prototype

πŸ“– Usage Example
// Create an object with a specific prototype
const proto = { greet() { return 'hi' } }
const child = Object.create(proto)
child.greet()   // β†’ 'hi'

Use Object.create() to create a new object with a specific prototype, enabling prototype-based inheritance.

Object.create()

Object.create(proto) creates a new object whose prototype is set to proto. This is a way to set up prototype-based inheritance without using classes.

Challenge: Implement createWithProto(proto) that returns a new object with proto as its prototype.

const animal = { speak() { return 'sound' } }
const dog = createWithProto(animal)
Object.getPrototypeOf(dog) === animal // β†’ true
πŸ’‘ Hints (2)
  • β€’ `Object.create(proto)` sets `proto` as the `[[Prototype]]` of the new object.
  • β€’ You can verify the link with `Object.getPrototypeOf(obj) === proto`.