0

I'm working in a large legacy codebase and just realized that someone did this:

Array.prototype.insertAt = function (i,el){ return this.splice(i,0,el); }
Array.prototype.deleteAt = function (i){return this.splice(i, 1); }

and that this is the reason i can't to this:

var derp = new Array();
derp.push('duh');
derp.push('what?');

for (var i in derp) {
    console.log(derp[i]);
}

Well, it's not that i can't do it, but if i do, i get unexpected results. That being instead of getting two lines of output (for 'duh' and 'what?') i get four. the last two being the two functions listed above.

I don't want to remove the original prototype functions (because god knows what depends on them), but i am wondering if there is a way to prevent the for loop from looping over the functions that were added.

3
  • 3
    for..in is purposed to iterate objects. Use a regular for loop instead. Commented Jan 29, 2016 at 16:45
  • 3
    Agree with using for loops - stackoverflow.com/questions/500504/… Commented Jan 29, 2016 at 16:46
  • 1
    I like both of these comments better than any of the official answers because i learned that i shouldn't be using for .. in in the first place. thanks Commented Jan 29, 2016 at 16:54

3 Answers 3

3

Method 1: Use a regular for (best supported)

for(var i=0;i<derp.length;i++){
    console.log(derp[i])
}

Method 2: Use hasOwnProperty

for(var i in arr){
 if(arr.hasOwnProperty(i)){
     console.log(i)
  }
}

Method 3: Use Object.defineProperty:

Object.defineProperty(Array.prototype, 'insertAt', {
    writeable: true,
    enumerable: false,
    value: Array.prototype.insertAt
})
Object.defineProperty(Array.prototype, 'deleteAt', {
    writeable: true,
    enumerable: false,
    value: Array.prototype.deleteAt
})

for(var i in derp){
    console.log(derp[i])
}

Which one you use depends on your browser support, last one prevents changing all for code written though

Sign up to request clarification or add additional context in comments.

1 Comment

The third method is perfect, this is exactly what i originally wanted. thanks
3

Use .forEach to iterate over an array or a regular for loop. for in is meant for object iteration and will include all properties on the prototype (which is why Object.keys(obj).forEach is preferred for object iteration as it does a hasOwnProperty check internally)

5 Comments

You're not considering legacy browser support for this method. This method is not available prior to IE9.
@JacobHeater: Just use a polyfill.
@RocketHazmat You're overcomplicating the solution.
a regular for loop isn't available prior to IE9? I believe I gave two options in my answer, and expounded upon enumerable prototype properties. OP has every ability to look further into each one of the methods listed.
@gabdallah Apologies! I should've read the answer more carefully. Correction: The Array.prototype.forEach method is not available prior to IE9. A for loop, of course, is!
0
for (var i in derp) {
    if(derp.hasOwnProperty(i)) 
        console.log(derp[i]);
}

But then, normal for loop is far better for Array than using for-in (which is actually for Object)

for (var i = 0; i < derp.length; i++) { 
    console.log(derp[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.