IDBKeyRange.includes() - Web APIs 编辑

The includes() method of the IDBKeyRange interface returns a boolean indicating whether a specified key is inside the key range.

Note:

This feature is available in Web Workers.

Syntax

var isIncluded = myKeyRange.includes(key)

Parameters

key The key you want to check for in your key range. This can be any type.

Return value

A Boolean.

Exceptions

This method may raise a DOMException of the following type:

AttributeDescription
DataErrorThe supplied key was not a valid key.

Example

var keyRangeValue = IDBKeyRange.bound('A', 'K', false, false);

var myResult = keyRangeValue.includes('F');
// Returns true

var myResult = keyRangeValue.includes('W');
// Returns false

Polyfill

The includes() method was added in the second edition of the Indexed DB specification. For browsers that do not support it, the following polyfill can be used.

IDBKeyRange.prototype.includes = IDBKeyRange.prototype.includes || function(key) {
  var r = this, c;
  if (r.lower !== undefined) {
    c = indexedDB.cmp(key, r.lower);
    if (r.lowerOpen && c <= 0) return false;
    if (!r.lowerOpen && c < 0) return false;
  }
  if (r.upper !== undefined) {
    c = indexedDB.cmp(key, r.upper);
    if (r.upperOpen && c >= 0) return false;
    if (!r.upperOpen && c > 0) return false;
  }
  return true;
};

Specifications

SpecificationStatusComment
Indexed Database API 2.0
The definition of 'includes()' in that specification.
Recommendation 
Indexed Database API 2.0
The definition of 'includes()' in that specification.
Recommendation 

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:65 次

字数:4668

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文