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

generator 应用 #20

Open
upgrace opened this issue Apr 28, 2020 · 0 comments
Open

generator 应用 #20

upgrace opened this issue Apr 28, 2020 · 0 comments

Comments

@upgrace
Copy link
Owner

upgrace commented Apr 28, 2020

 核心: 能够暂停和恢复执行任务,是其能实现异步任务的根本原因
 应用: 将数组扁平化操作
    function typeOf (obj) {
      const toString = Object.prototype.toString
      const map = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object',
        '[object Symbol]': 'symbol',
        '[object Set]': 'set',
        '[object Map]': 'map'
      }
      return map[toString.call(obj)]
    }

      function* flat(n) {
          function isArray(arr) {
            return ['array', 'set'].includes(typeOf(arr))
          }
          if (isArray(n)) {
            for (let i of n) {
              if (isArray(i)) {
                yield* flat(i)
              } else {
                yield i
              }
            }
          }
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant