Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lodash源码分析之some #214

Open
HeftyKoo opened this issue Apr 25, 2020 · 0 comments
Open

lodash源码分析之some #214

HeftyKoo opened this issue Apr 25, 2020 · 0 comments
Labels
api 暴露出来的接口 系列文章

Comments

@HeftyKoo
Copy link
Owner

本文为读 lodash 源码的第二百一十三篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash

gitbook也会同步仓库的更新,gitbook地址:pocket-lodash

源码分析

some 来数组的 some 方法的作用一样,可以传入断言函数,some 会遍历数组,在遍历的过程中会调用断言函数,只要断言函数返回真值,就会中止遍历,并且返回结果 true ,否则得到结果 false

源码如下:

function some(array, predicate) {
  let index = -1
  const length = array == null ? 0 : array.length

  while (++index < length) {
    if (predicate(array[index], index, array)) {
      return true
    }
  }
  return false
}

可以看到,在遍历的过程中,会将当前值,当前索引和原数组传给断言函数 predicate

如果 array 为空,得到的结果为 false ,因为根本没有机会调用断言函数。

参考资料

MDN: Object.keys()

License

署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)

最后,所有文章都会同步发送到微信公众号上,欢迎关注,欢迎提意见:

作者:对角另一面

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api 暴露出来的接口 系列文章
Projects
None yet
Development

No branches or pull requests

1 participant