weakMap.set() 是 JavaScript 中的內置函數,用於將具有特定鍵和值的新元素設置為 WeakMap 對象。
用法:
weakMap.set(key, value);
參數:它采用參數“key”,它是要設置到WeakMap對象的元素的鍵,參數“value”是要設置到WeakMap對象的元素的值。
返回值:它返回WeakMap對象。
以下是weakMap.set()方法的示例。
示例 1:
javascript
function gfg() {
const weakmap1 = new WeakMap();
const key1 = {};
const key2 = {};
const key3 = {};
weakmap1.set(key1, 'G');
weakmap1.set(key2, 'F');
weakmap1.set(key3, 'G');
document.write(weakmap1.get(key1)
+weakmap1.get(key2)
+weakmap1.get(key3));
}
輸出:
GFG
示例 2:
javascript
// 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
console.log(weakmap1.get(key1));
console.log(weakmap1.get(key2));
輸出:
GeeksForGeeks gfg
示例 3:
javascript
// 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
console.log(weakmap1.get(key1));
console.log(weakmap1.get(key2));
console.log(weakmap1.get(key3));
console.log(weakmap1.get(key4));
console.log(weakmap1.get(key5));
console.log(weakmap1.get(key6));
輸出:
GeeksForGeeks gfg GfG is a cse portal 12345 @#$% 1.34
支持的瀏覽器:
- 穀歌瀏覽器 36 及以上版本
- 邊 12 及以上
- 火狐瀏覽器 6 及以上版本
- Internet Explorer 11 及更高版本
- Opera 23 及以上
- Safari 8 及以上版本
我們有 JavascriptweakMap 方法的完整列表,要檢查這些方法,請閱讀這篇JavaScript WeakMap Complete Reference 文章。
我們有一份關於 Javascript 的備忘單,其中涵蓋了 Javascript 的所有重要主題,請查看這些主題Javascript Cheat Sheet-A JavaScript 基本指南.
相關用法
- JavaScript weakMap get()用法及代碼示例
- JavaScript weakMap has()用法及代碼示例
- JavaScript weakMap delete()用法及代碼示例
- JavaScript weakSet add()用法及代碼示例
- JavaScript weakSet has()用法及代碼示例
- JavaScript weakSet delete()用法及代碼示例
- JavaScript window.close()用法及代碼示例
- JavaScript window.open()用法及代碼示例
- JavaScript Math cosh()用法及代碼示例
- JavaScript Math sinh()用法及代碼示例
- JavaScript Math sin()用法及代碼示例
- JavaScript Math cos()用法及代碼示例
- JavaScript Math tan()用法及代碼示例
- JavaScript Math abs()用法及代碼示例
- JavaScript Math pow()用法及代碼示例
- JavaScript Math asin()用法及代碼示例
- JavaScript Math acos()用法及代碼示例
- JavaScript Math atan()用法及代碼示例
- JavaScript Math ceil()用法及代碼示例
- JavaScript Math floor()用法及代碼示例
- JavaScript Math round()用法及代碼示例
- JavaScript Math trunc()用法及代碼示例
- JavaScript Math max()用法及代碼示例
- JavaScript Math min()用法及代碼示例
- JavaScript Math sqrt()用法及代碼示例
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript weakMap set() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。