weakMap.set()是JavaScript中的內置函數,用於為WeakMap對象設置具有特定鍵和值的新元素。句法:
weakMap.set(key, value);
參數:它接受參數“key”,這是要設置為WeakMap對象的元素的鍵,而參數“value”是要設置為WeakMap對象的元素的值。
返回值:它返回WeakMap對象。
代碼1:
<script>
// creating WeakMap() object
const weakmap1 = new WeakMap();
// Creating some keys
const key1 = {};
const key2 = {};
// Setting key and value to the object.
weakmap1.set(key1, 'GeeksForGeeks');
weakmap1.set(key2, 'gfg');
// Returning the set values
document.write(weakmap1.get(key1) +"<br>");
document.write(weakmap1.get(key2));
</script>
輸出:
GeeksForGeeks gfg
代碼2:
<script>
// creating WeakMap() object
const weakmap1 = new WeakMap();
// Creating some keys
const key1 = {};
const key2 = {};
const key3 = {};
const key4 = {};
const key5 = {};
const key6 = {};
// Setting key and value to the object.
weakmap1.set(key1, 'GeeksForGeeks');
weakmap1.set(key2, 'gfg');
weakmap1.set(key3, 'GfG is a cse portal');
weakmap1.set(key4, '12345');
weakmap1.set(key5, '@#$%');
weakmap1.set(key6, '1.34');
// Returning the set values
document.write(weakmap1.get(key1) +"<br>");
document.write(weakmap1.get(key2) +"<br>");
document.write(weakmap1.get(key3) +"<br>");
document.write(weakmap1.get(key4) +"<br>");
document.write(weakmap1.get(key5) +"<br>");
document.write(weakmap1.get(key6));
</script>
輸出:
GeeksForGeeks gfg GfG is a cse portal 12345 @#$% 1.34
相關用法
- Javascript typedArray.from()用法及代碼示例
- Javascript typedArray.of()用法及代碼示例
- Javascript weakSet.has()用法及代碼示例
- Javascript typedArray.map()用法及代碼示例
- Javascript weakSet.add()用法及代碼示例
- Javascript weakMap.has()用法及代碼示例
- Javascript typedArray.every()用法及代碼示例
- Javascript getPrototypeOf()用法及代碼示例
- Javascript uneval()用法及代碼示例
- Javascript parseInt()用法及代碼示例
- Javascript parseFloat()用法及代碼示例
- Javascript unescape()用法及代碼示例
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript | weakMap.set() with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。