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

ES6之迭代器 #37

Open
Saikikoko opened this issue Mar 18, 2021 · 0 comments
Open

ES6之迭代器 #37

Saikikoko opened this issue Mar 18, 2021 · 0 comments
Labels
Js knowledge of Javascript

Comments

@Saikikoko
Copy link
Owner

迭代器

什么是迭代器?迭代器就是一个具有next方法的对象,每次调用next返回一个对象,该对象具有两个属性,一个value值和一个done值,done为true表示迭代结束。
模拟一个迭代器

function createIterator(items) {
  var len = items.length
  var i = 0
  return {
    next: function() {
      value = items[i++]
      done = i > len ? true : false
      return {
        value: value, 
        done: done
      }
    }
  }
}

const iterate = createIterator([1,2,3])

console.log(iterate.next())
console.log(iterate.next())
console.log(iterate.next())
console.log(iterate.next())
@Saikikoko Saikikoko added the Js knowledge of Javascript label Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Js knowledge of Javascript
Projects
None yet
Development

No branches or pull requests

1 participant