javascript - Using for...in and instanceof for parsing objects -



javascript - Using for...in and instanceof for parsing objects -

i'm attempting loop through object , utilize different functions on each different sub-object based on it's type. instanceof seemed way identify non-built in types, attempted utilize for...in; however, i'm not getting expected output.

here sample fiddle illustrate issue: http://jsfiddle.net/zv230dvl/2/

function testobject(){} function testcontainer(objects){ this.zero = objects[0]; this.one = objects[1]; this.two = objects[2]; } var singleobject = new testobject(); var container = new testcontainer( [new testobject(), new testobject(), new testobject()] ); ... //testing if single object functions expected out(singleobject instanceof testobject); //testing if straight referencing object //in container functions expected out(container.zero instanceof testobject); //iterating through container object... for(i in container){ out(i instanceof testobject); }

the output is

true true false false false

first, appropriate method this?

second, why variable in for...in loop behave in manner?

no jquery please.

when iterate object for..in, loop variable have key, in string type. so, should checking this

for (i in container) { out(container[i] instanceof testobject); }

now, using object corresponding key i in container. change, items evaluate true.

note: for..in supposed give inherited properties well. so, normal looping pattern when utilize for..in object is

for (i in container) { if (container.hasownproperty(i)) { out(container[i] instanceof testobject); } }

this create sure that, properties straight defined in object checked.

javascript object

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -