4

Why creating custom prototype functions like:

Array.prototype.clone = function(){
    return JSON.parse(JSON.stringify(this));
}

is making them visible when iterating over a for loop?

For example: enter image description here

It's clear that I have an array with 7 arrays inside, but for some reason it is considering all my custom functions when inside the loop. Why? How can I prevent it?

OBS: I am applying to some sort of javascript contest, which takes my algorithm and plays against other players. This loop, is inside the runner, so please consider that changing the way the iterations are processed is not an option.

This is breaking the runner as it tries to execute some code with the columns thinking that my custom function are included on it.

However, looking at their code, I noticed that it is possible to prevent this from happening as they also edit/create Array.prototype functions.

6
  • Consider using jsfiddle.net to provide the minimum code needed to reproduce the problem. Commented Sep 25, 2015 at 1:29
  • possible duplicate of Javascript: hiding prototype methods in for loop? Commented Sep 25, 2015 at 1:31
  • @LJᛃ I understand the similarity, but I don't think it's a duplicate as I don't want to change the loop (as I said on the question, I can't). I want is to my custom function to be hidden even on that case. Commented Sep 25, 2015 at 1:39
  • @PatrickBard well check the second answer there, its the same as the second part of the answer you got: make your methods non enumerable. Commented Sep 25, 2015 at 1:40
  • Have a read through object.prototype methods. I could tell you which one but you would have to give me the prize. Commented Sep 25, 2015 at 3:41

1 Answer 1

3

Don't use for...in loops to iterate over an array. for...in loops enumerate all of the properties of an object, and since your new prototype function is enumerable, it will be listed as well. The way to avoid this is to use array.hasOwnProperty, but why do that when you can iterate over the array correctly with a regular for loop? It'll be faster and use less code:

for (var i = 0; i < this.matrix.length; i++) {
    ...
}

To make your new function not appear when enumerating an object's properties, you need to make it not enumerable:

Object.defineProperty(Array.prototype, 'clone', {
    enumerable: false,
    value: function(obj) {
        return JSON.parse(JSON.stringify(obj));
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

I see, but as I said, I don't have control of the code they use. Inside their runner there is a for...in loop, and my code is breaking it.
@PatrickBard: Then either don't modify the prototype, or mark your function as not enumerable. I'd go with the former.

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.