Path: csiph.com!3.eu.feeder.erje.net!feeder.erje.net!news.albasani.net!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Object prop missing? Date: Thu, 13 Jun 2019 13:05:20 +0200 Organization: PointedEars Software (PES) Lines: 90 Message-ID: <1580036.FruvyXNdhd@PointedEars.de> References: <12600445.cR47uQ6sgG@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.albasani.net gC5zUnikuzi+/VtlOywPBLLp2Gyz2Nus+2EA9qbVjWESMgq0N8n3qtkud7/swQMY2FiclRWtoO1JVlthk3/gfRLB2SdU92btn+2n710Busss1N54ErWEPy4MOb1ix3qg NNTP-Posting-Date: Thu, 13 Jun 2019 11:05:21 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="P4Ni/81c4LoxtgNt17sZOedz4XxsKqAgQ2qQlDNjM7gHmC3DDmXRyzQp9Lpk7CZasp9BSfrEfchOeoaiMNMH/x9KyJ8JOEyj70sx/usEamUcDAgz8xwYnIyrkj8n0s4j"; mail-complaints-to="abuse@albasani.net" User-Agent: KNode/4.14.10 Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. var options = [ > {}, > {"id": "opt1", "on": false }, > {"id": "opt2", "on": false }, > {"id": "opt3", "on": false }, > {"id": "opt4", "on": false }, > {"id": "opt5", "on": false } > ]; > > for (var i = 6; i > 0; i--) { > // add stuff to options elements > options[i].h = 28; > } > > showOptions(); > > function showOptions() { > console.log(options[1]); // 1. > console.log(options[1].h) // 2. > console.log(typeof options[1]); // 3. > > // code that relies on .h values > } > > In Windows Chrome 75.0.3770.80 (64-bit): > > 1. > > Object > > when opened > > v Object > > elem: div#opt1.opt > h: 28 > id: "opt1" > maxw: 548 > on: false > > pos: {x: 46} > > txt: p > __proto__: Object > > 2. undefined > > 3. object > > h is in options[1] but when I try to get the value it's not. I cannot reproduce your results. If I run your code in | > navigator.userAgent | <- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) | Chrome/73.0.3683.75 Safari/537.36" I get | Uncaught TypeError: Cannot set property 'h' of undefined | at :12:17 because your code is logically in error: The referred Array instance has 6 elements, so it does NOT have an element with index 6 (indexes start at 0) and “options[6]” is evaluated to the *primitive* (non-object) “undefined” value. Note that for (var i = 6; i > 0; i--) is NOT equivalent to for (var i = 6; i--;) The former checks whether the value of “i” is *greater than* zero; if so, the Block statement is executed. *After* that “i” is decremented. By contrast, the latter checks whether the value of “i” is *not equal to* zero, and decrements it *in any case* (to produce an *existing* index) *before* the Block statement is executed (only if “i” was not equal to zero). The equivalent code would be (confusingly; do not do that) for (var i = 6; i-- > 0;) -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.