Skip to content

Instantly share code, notes, and snippets.

@till
Created August 28, 2010 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save till/555278 to your computer and use it in GitHub Desktop.
Save till/555278 to your computer and use it in GitHub Desktop.
/*
save the following file as 'array.js' and run with: node array.js
*/
sys = require('sys');
Array.prototype.foo = function () { return 'foo'; }
Array.prototype.bar = function () { return 'bar'; }
var testArray = [1, 2, 3, 4, 5];
for (var x in testArray) {
sys.puts(testArray[x]);
}
@janl
Copy link

janl commented Aug 28, 2010

for (var x in testArray) {
if(testArray.hasOwnProperty(x)) {
sys.puts(testArray[x]);
}
}

@till
Copy link
Author

till commented Aug 28, 2010

Fix:

for (var x = 0; x < testArray.length; x++) { }

@janl
Copy link

janl commented Aug 28, 2010

testArray.forEach(function(element) {
sys.puts(element);
});

@andrerom
Copy link

for (var x = 0; x < testArray.length; x++) { }

for (var x = 0, l = testArray.length; x < l; x++) { };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment