Drycodez Posted September 9, 2011 Share Posted September 9, 2011 I know about for in loop and what it does, but my prob is that: i don't knw why there's a variable (x) inside the for-in-loop, followed by 'in'. I just dont knw how to Xplain my prob, but the stuff i really confusing!person={name:'o.s'} For(x in person)//oscan some one please explain DIS confusing for-in-loop? Link to comment Share on other sites More sharing options...
thescientist Posted September 9, 2011 Share Posted September 9, 2011 well, if you alerted x, you would get the key(s) of an object, in this case, name. If you alerted person[x], you would get o.s. The convenience provided by this loop is that it allows you to iterate through an object without having to know it's keys. It's not like an array where you can index its keys numerically. The reason x is used is just like in a regular loop, it holds the current key/index that it is being iterated on. for(var i = 0; i < array.length; i++){ console.log(array[i]);}; same concept. Link to comment Share on other sites More sharing options...
Ingolme Posted September 9, 2011 Share Posted September 9, 2011 In the for...in loop, the lefthand operand of the in operator is a memory space that takes the name of one of the properties of the righthand operand on each iteration. var x; var obj = { "a" : 1, "b" : 2};// Begin a trace:for(x in obj) { console.log("x = " + x); console.log("obj." + x + " = " + obj[x]);} The output will look like this:x = aobj.a = 1x = bobj.b = 2 Link to comment Share on other sites More sharing options...
ShadowMage Posted September 9, 2011 Share Posted September 9, 2011 The output will look like this:x = aobj[x] = 1x = bobj[x] = 2 Not quite. :Pconsole.log("obj." + x + " = " + obj[x]);produces:obj.a = 1 Link to comment Share on other sites More sharing options...
Ingolme Posted September 9, 2011 Share Posted September 9, 2011 I was just editing my post after I noticed my mistake. Link to comment Share on other sites More sharing options...
thescientist Posted September 9, 2011 Share Posted September 9, 2011 am I missing something SM? I don't see what is wrong with Ingolme's example... I'm not sure where you got the square brackets from.... unless mods can edit their posts without an edited post message showing. Link to comment Share on other sites More sharing options...
Ingolme Posted September 9, 2011 Share Posted September 9, 2011 Yeah, I can edit posts and have them appear as if it was always like that. I'll fix it to avoid confusion. Link to comment Share on other sites More sharing options...
thescientist Posted September 9, 2011 Share Posted September 9, 2011 sneaky sneaky Link to comment Share on other sites More sharing options...
Drycodez Posted September 9, 2011 Author Share Posted September 9, 2011 Thanks Guys, i think am begining to get it! I ll google more info[/b] about it! But what does: console.log() represent? Is that a func or what? Link to comment Share on other sites More sharing options...
Ingolme Posted September 9, 2011 Share Posted September 9, 2011 Most browsers today support the console.log() method. It just writes to the browser's Javascript console, or to Firebug. It's great for debugging because it doesn't modify the page and it's less bothersome than an alert dialog. Link to comment Share on other sites More sharing options...
thescientist Posted September 9, 2011 Share Posted September 9, 2011 try it. it's an alternative to using alert so that you can effectively trace and track the logic of your applications. It's cross browser compatible, even in IE (7+). In my experience getting it to work in IE7, I've just made sure to declare it globally. I include it in my main script file for all my webapp's just to be on the safe side. if (typeof console === 'undefined') { window.console = {};}; if (!console.log) { console.log = function () {}; console.log('INIT console logging');}; Link to comment Share on other sites More sharing options...
Drycodez Posted September 9, 2011 Author Share Posted September 9, 2011 Ok! Thanks guys, I realy appreciate. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.