object.create(null)
时间: 2023-04-24 14:01:55 浏览: 108
object.create(null)是创建一个没有原型链的空对象。这意味着它不会继承任何属性和方法,也不会有任何内置的属性和方法。这种对象通常用于存储数据,因为它不会受到原型链的干扰,可以确保数据的纯净性和完整性。
相关问题
Object.Create(null)
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.
Object.create(Book)和 Object.create(Book.prototype)有什么区别
Object.create(Book)和Object.create(Book.prototype)的区别在于创建的新对象的原型链不同。
当使用Object.create(Book)时,创建的新对象的原型是Book对象本身,它继承了Book对象的属性和方法。例如,如果Book对象有一个属性a,那么通过Object.create(Book)创建的新对象也会有属性a。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Object.create() 方法](https://blog.csdn.net/qq_43199014/article/details/126010399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [Object.create()是怎样建立原型链的](https://blog.csdn.net/qzw752890913/article/details/105586523)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)