Skip to content

why can not use for...of in this time?

Hill Liu edited this page Dec 30, 2019 · 4 revisions

What is about it?

Because bebel will transpiler code to "Symbol.iterator", it is not support on IE11.

babel for…​of

before babel

for (var i of foo) {}

after babel

for (var _iterator = foo, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  var _ref;

  if (_isArray) {
    if (_i >= _iterator.length) break;
    _ref = _iterator[_i++];
  } else {
    _i = _iterator.next();
    if (_i.done) break;
    _ref = _i.value;
  }

  var i = _ref;
}

better usage.

const keys = Object.keys;
const myHash = {foo1:bar1, foo2:bar2};
keys(myHash).forEach(key=>myHah[key]);