Created
September 19, 2009 20:49
-
-
Save eligrey/189579 to your computer and use it in GitHub Desktop.
Implicit filtering of properties that are not an object's own in `for (property in object)' and `for each (value in object)' loops.
This file contains 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
/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */ | |
// usage example: http://gist.github.com/189580 | |
"use strict"; | |
(function () { | |
var objProto = Object.prototype, | |
hasOwnProp = objProto.hasOwnProperty, | |
iterMethod = "__iterator__", | |
iterator = function (flag) { | |
var prop; | |
delete objProto[iterMethod]; | |
for (prop in this) { | |
if (hasOwnProp.call(this, prop)) { | |
yield flag ? prop : this[prop]; | |
} | |
} | |
objProto[iterMethod] = iterator; | |
}; | |
objProto[iterMethod] = iterator; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment