has() 方法對一個指定值元素在 Set 物件中的存在與否回傳一個布林值。

語法

mySet.has(value);

參數

value
要測試是否存在在 Set 中的值。

回傳值

回傳 true 如果給定值存在在 Set 物件中;反之回傳 false

Note: 技術上來說,has() 使用了 sameValueZero 算法來判斷給定元素的存在與否。

範例

使用 has 方法

var mySet = new Set();
mySet.add('foo');

mySet.has('foo');  // returns true
mySet.has('bar');  // returns false

var set1 = new Set();
var obj1 = {'key1': 1};
set1.add(obj1);

set1.has(obj1);        // returns true
set1.has({'key1': 1}); // returns false because they are different object references
set1.add({'key1': 1}); // now set1 contains 2 entries

規範

規範 狀態
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Set.prototype.has' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Set.prototype.has' in that specification.
Draft  

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
hasChrome Full support 38Edge Full support 12Firefox Full support 13IE Full support 11Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support

另見

文件標籤與貢獻者

此頁面的貢獻者: hmysjiang
最近更新: hmysjiang,