Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialian izaguirre
3,220 PointsQuestion regarding why some info on Performance using Objects For In Loop was not mentioned in this video?
Hi, after looking at this video, I went on to read a little online about it to help fill in some gaps. Then I noticed a note on performance that seems to be important. It suggested to always use the .hasOwnProperty
method when using for-in
loops, in order to make sure you loop through the the actual properties key-value pairs your calling and not any of the built-in object/meta-information the parent object has by default.
So I am wondering why something like this would then not be mentioned in this video?
His example of:
var person = {
name : 'Sarah',
age : 35
};
for ( var key in person ) {
console.log( key, ': ', person[key] );
}
Apparently, would have been improved if it was written as:
var person = {
name : 'Sarah',
age : 35
};
for ( var key in person ) {
if ( person.hasOwnProperty(key) ) {
console.log( key, ': ', person[key] );
}
}
Link with more info on Mozilla: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
Also Is this .hasOwnProperty()
method something I should always use in for-in loops or should I only use it sometimes?
1 Answer
Jonathan Grieve
Treehouse Moderator 91,253 PointsI think it's something that can be incorporated into larger projects. Something to build on when you get further into your JavaScript development journey. But in terms of the scripts you're building in this course I don't think we have too much to worry about in terms of memory drain, unless of course, you make an infinite loop!
It might be worth adding something about it to the teachers notes though for bigger projects if Dave McFarland agrees! :-) But for this course, this is introducing you to the very basics of what loops can do! :-)
ian izaguirre
3,220 Pointsian izaguirre
3,220 Pointsokay thank you for clearing that up.