weakMap.get()是JavaScript中的內置函數,用於從對象WeakMap返回特定元素。
用法:
weakMap.get(key);
參數:它接受參數“key”,它是將從對象弱映射中返回的元素的鍵。
返回值:它返回與WeakMap對象中的特定鍵關聯的元素,如果找不到該鍵,則返回未定義。
代碼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對象的末尾設置,因此輸出未定義。
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript | weakMap.get()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。