Created
February 20, 2014 19:41
-
-
Save pvdz/9121615 to your computer and use it in GitHub Desktop.
for-in rewrite rules.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
Have this working now. Result is a bit different:
Basically hacked the ultimate key assignment into the
if
condition so that I wouldn't have to bother with wrapping the for-statement in block brackets. Could still do that of course, just more work for no other reason than human legibility. Which is already zilch :p