weakMap.delete()是JavaScript中的內置函數,可用於從對象WeakMap中刪除特定元素。
用法:
weakMap.delete(key);
參數:它接受參數“key”,這是將從對象weakMap中刪除的元素的鍵。
返回值:如果該元素已從weakMap對象中刪除,則返回true;如果該鍵不存在於weakMap對象中,則返回false。
代碼1:
<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:
<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對象的末尾。
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript | weakMap.delete()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。