下面是一个例子 懦夫。get()() 方法
null
- 例子:
<script>
function
gfg() {
const weakmap1 =
new
WeakMap();
const key1 = {};
weakmap1.set(key1, 12);
document.write(weakmap1.get(key1));
}
gfg();
</script>
- 输出:
12
这个 懦夫。得到() 是JavaScript中的一个内置函数,用于从对象WeakMap返回特定元素。
语法:
weakMap.get(key);
参数: 它接受一个参数“key”,它是从对象weakmap返回的元素的key。
返回值: 它返回与WeakMap对象中特定键关联的元素,如果找不到键,则返回undefined。
例子:
Input: weakmap1.get(key1) Output: 42
显示weakmap()函数工作的JavaScript代码: 代码#1:
<script> // Creating a WeakMap() object const weakmap1 = new WeakMap(); // Creating a key "key1" const key1 = {}; // setting value 42 with key "key1" in the // object weakMap weakmap1.set(key1, 42); // Getting the specified elements i.e, 42 document.write(weakmap1.get(key1)); </script> |
输出:
42
代码#2:
<script> // Creating a WeakMap() object const weakmap1 = new WeakMap(); // Creating a key "key1" const key1 = {}; // Getting the specified elements document.write(weakmap1.get(key1)); </script> |
输出:
undefined
此处输出未定义,因为键“key1”未设置在weakMap对象的末尾。
支持的浏览器:
- 谷歌Chrome 36及以上版本
- 边缘12及以上
- Firefox 6及以上版本
- Internet Explorer 11及以上版本
- Opera 23及以上
- Safari 8及以上
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END