Skip to content

Instantly share code, notes, and snippets.

@eligrey
Created September 19, 2009 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eligrey/189579 to your computer and use it in GitHub Desktop.
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.
/*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