Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Use standard for when iterating over array items and add hasOwnProperty check for objects #45

Open
Maluen opened this issue May 21, 2013 · 2 comments

Comments

@Maluen
Copy link
Contributor

Maluen commented May 21, 2013

Otherwise strange things happen if the application adds properties to the Array prototype, another solution is to insert an "hasOwnProperty" check inside the "for in" loop, but the former is a better fix for arrays imho.

For example, in the loop function, starting from row 342:

for(var i in lengthsubjects){

    var subj = lengthsubjects[i];
    var difference = getObjDiff(subj.obj[subj.prop], subj.actual);

    // ...
};

In this case, the loop will iterate also over non owned-properties, i.e. property added to the Array prototype.

So the code should be changed to:

for(var i=0,l=lengthsubjects.length; i<l; i++) {

    var subj = lengthsubjects[i];
    var difference = getObjDiff(subj.obj[subj.prop], subj.actual);

    // ...
};

Same apply when iterating over objects, but in this case an "obj.hasOwnProperty(prop)" check inside the "for in" loop is required.

@melanke
Copy link
Owner

melanke commented May 21, 2013

good point

I will fix it

thx

@gregkaczan
Copy link

Indeed "hasOwnProperty" is mandatory, otherwise i got error: Uncaught TypeError: Cannot read property 'undefined' of undefined

Maluen added a commit to Maluen/Watch.JS that referenced this issue Nov 18, 2013
- Removed JSON.stringify (issue melanke#37)
- Added level parameter (issue melanke#30)
- Replaced 'for in'  with standard for when iterating over arrays, added the hasOwnProperty check otherwise (issue melanke#45)
- Added support for checking arrays in the getObjDiff function
- Removed usage of addNRemove when level is 0
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants