Object.Create(null)
时间: 2024-05-09 18:21:26 浏览: 8
Object.create(null) creates a new object with no prototype. This means that the new object created will not inherit any properties or methods from the Object prototype. This can be useful in scenarios where you want to create a plain object without any default properties or methods.
For example, if you want to create an object that does not have any default properties or methods and you want to add properties and methods to it manually, you can use Object.create(null) to create a new object.
Here's an example:
```
const myObject = Object.create(null);
myObject.name = 'John';
myObject.age = 30;
console.log(myObject); // { name: 'John', age: 30 }
```
In this example, we create a new object `myObject` using `Object.create(null)`. We then add two properties `name` and `age` to the object manually. Since `myObject` has no prototype, it does not inherit any default properties or methods from the Object prototype. The output of the `console.log` statement shows that `myObject` only has the two properties we added to it.