Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created February 20, 2014 19:41
Show Gist options
  • Save pvdz/9121613 to your computer and use it in GitHub Desktop.
Save pvdz/9121613 to your computer and use it in GitHub Desktop.
for-in rewrite rules.
// var version, can only have one var with a limited initializer
// can safely reuse the new var because it can't have side effects
for (var key=init in o) stmt;
// =>
var key = init, _γ_keys = [], _γ_expro = expr;
for (key in _γ_expro) _γ_keys.push(key);
while (_γ_keys.length) if (expro.hasOwnProperty(key = _γ_keys.shift())) stmt;
// non-var version, lhs can be any single expression
// we have to create our own temp var because lhs is not "safe"
// makes hasOwnProp check more tedious because we have to postpone key assignment
for (lhs in expr) stmt;
// =>
lhs;
var _γ_key, _γ_keys = [], _γ_expr_o = expr, _γ_tkey;
for (_γ_key in _γ_expro) _γ_keys.push(_γ_key);
while (_γ_keys.length) if (_γ_expro.hasOwnProperty(_γ_tkey = _γ_keys.shift())) { lhs = _γ_tkey; stmt; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment