Object.create
JS Functions
Easy
10 min
prototypes
objects
polyfills
Implement objectCreate(proto, propertiesObject), which behaves like Object.create. It returns a new, empty object whose prototype is proto, optionally with properties defined from propertiesObject.
const animal = {
speak() {
return `${this.name} makes a sound`;
},
};
const dog = objectCreate(animal);
dog.name = 'Rex';
dog.speak(); // 'Rex makes a sound'
Object.getPrototypeOf(dog) === animal; // true
const dict = objectCreate(null); // no prototype at all
'toString' in dict; // false
const point = objectCreate(Object.prototype, {
x: { value: 1, enumerable: true },
y: { value: 2 }, // non-enumerable, non-writable by default
});
Object.keys(point); // ['x']
Requirements
- Do not use
Object.create. protomust be an object, a function, ornull. Anything else throws aTypeError.objectCreate(null)returns an object with no prototype.- If
propertiesObjectis provided and notundefined, define its own enumerable properties as property descriptors on the new object, exactly likeObject.defineProperties. You may useObject.definePropertiesandObject.setPrototypeOf.
Follow-up
- How did ES5-era polyfills implement this without
Object.setPrototypeOf, and what couldn't they support? - When would you reach for
Object.create(null)in real code?
Spotted a mistake or a rights concern? Report an issue