下面是一个例子 懦夫。删除() 方法
null
- 例子:
javascript
<script> function gfg() { const weakmap = new WeakMap(); const key = {}; weakmap.set(key, 6); document.write(weakmap. delete (key)); } gfg(); </script> |
- 输出:
true
这个 懦夫。删除() 是JavaScript中的一个内置函数,用于从对象映射中删除特定元素。 语法:
weakMap.delete(key);
参数: 它接受一个参数“key”,它是将从对象weakMap中删除的元素的键。 返回值: 如果该元素已从weakMap对象中删除,则返回true;如果weakMap对象中不存在该键,则返回false。 例子:
Input: weakmap1.delete(key1)Output: true
显示此函数工作的JavaScript代码: 代码#1:
javascript
<script> // Creating a WeakMap() object const weakmap1 = new WeakMap(); // Creating a key "key1" const key1 = {}; // Setting the value 6 with key1 to the // the end of weakMap object weakmap1.set(key1, 6); // Deleting key of the element from // the weakMap object document.write(weakmap1. delete (key1)); </script> |
输出:
true
这里的输出为true,这意味着元素的键已被成功删除。 代码#2:
javascript
<script> // Creating a WeakMap() object const weakmap1 = new WeakMap(); // Creating a key "key1" const key1 = {}; // Deleting key of the element from // the weakMap object document.write(weakmap1. delete (key1)); </script> |
输出:
false
这里的输出为false,因为带有任何值的键“key1”尚未设置到weakMap对象的末尾。
支持的浏览器:
- 铬36及以上
- 边缘12及以上
- Firefox 6及以上版本
- Internet Explorer 11及以上版本
- Opera 23及以上
- Safari 8及以上
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END