创建新对象时:为何选用Object.create(Object.prototype)而非Object.create(Object)
Great question! Let's unpack the difference between these two lines of code step by step—once you see what each does under the hood, you'll immediately get why Object.create(Object.prototype) is the right pick for creating a plain, standard object.
First, let's recall what Object.create() does: it creates a new object, and sets the first argument as the prototype of that new object. So the key here is understanding what Object vs Object.prototype actually is.
What is Object?
Object is a built-in constructor function in JavaScript. Yes, it's a function—not just a plain object. Its own prototype is Function.prototype (since all functions inherit from that), and it has properties like name (set to "Object"), length (set to 1, because the Object constructor expects one argument), plus all the methods from Function.prototype like call(), apply(), bind().
What is Object.prototype?
Object.prototype is the top of the prototype chain for all plain JavaScript objects. It's where methods like toString(), hasOwnProperty(), valueOf() live—these are the methods we expect every regular object to have.
Let's look at var task = Object.create(Object)
When you do this, you're setting the prototype of task to the Object constructor function. That means:
taskwill inherit all properties/methods fromObject(the function) andFunction.prototype.- If you check
task instanceof Function, it will returntrue—becauseFunction.prototypeis in its prototype chain. That's weird if you just wanted a plain data object! - You'll have access to function-specific methods like
task.call()ortask.bind()on your "object", which you almost never need for a regular object.
Now var task = Object.create(Object.prototype)
This is what you want for a standard plain object:
- The prototype of
taskisObject.prototype, so it inherits all the standard object methods (toString, hasOwnProperty, etc.)—just like an object created with{}(the object literal syntax). task instanceof Objectreturnstrue(as expected), andtask instanceof Functionreturnsfalse—which is exactly what we want for a regular data object.- This object has no extra function-related baggage; it's a clean, standard object ready to hold your data.
A quick example to see the difference
const task1 = Object.create(Object); console.log(task1 instanceof Function); // true (unexpected!) console.log(task1.name); // "Object" (inherited from the Object constructor) const task2 = Object.create(Object.prototype); console.log(task2 instanceof Function); // false (correct!) console.log(task2.name); // undefined (no inherited name property)
Wrap-up
Use Object.create(Object.prototype) when you want a plain object with the standard prototype chain. Using Object.create(Object) creates an object that inherits from a function, which is almost never what you intend when creating a regular data object—it adds unnecessary function-related properties and behavior that can lead to confusion or bugs.
内容的提问来源于stack exchange,提问作者Bikash

