On Sun, Feb 12, 2012 at 3:40 PM, Joe Developer <[email protected]>wrote:
> Requests for enlightenment: > > On Sun, Feb 12, 2012 at 7:42 PM, Shimon Doodkin <[email protected]>wrote: > >> for (var key in myobj) must be accompanied with >> if(Object.hasOwnProperty.call(myobj,key)) >> >> example: >> >> for (var key in myobject) { >> if(Object.hasOwnProperty.call(myobject,key)){ >> ... >> } >> } >> >> myobject.forEach(function(){...}) already does this. >> >> >> more: >> >> You could do : myobj.hasOwnProperty(key) But then if myobj is NULL it >> will throw an error. >> > > Wouldn't it be more efficient to simply check if myobj is null before > trying to iterate over it? > In similar vein, wouldn't it be better to not declare the var inside the > for() in this instance? > Doesn't matter where you declare a variable, V8 will move you declaration to the top of the current scope. > I realize that for the vast majority of cases the contents of the loop > will be far heavier than the scaffolding involved, but since we are > presenting 'best practice' scenarios for something that I would imagine > would often sit in a util function somewhere... > > I have created: http://jsperf.com/obj-property-iteration in case it might > have value, if nothing else to have a place to tack on various approaches. > > > So you use the method hasOwnProperty from the prototype object. >> and you call it with changing the this object of it to your object. >> >> Object.hasOwnProperty.call(myobj,key) >> >> Most of you probably know this, but sometimes I encounter modules that >> don't do this. >> And they do unexpected errors. >> >> -- >> Job Board: http://jobs.nodejs.org/ >> Posting guidelines: >> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >> You received this message because you are subscribed to the Google >> Groups "nodejs" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected] >> For more options, visit this group at >> http://groups.google.com/group/nodejs?hl=en?hl=en >> > > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
