Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.mixmin.net!news2.arglkargh.de!diablo1.news.osn.de!news.osn.de!diablo2.news.osn.de!news.belwue.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1934328.aqSPkdTlGX@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Mon, 23 Jan 2012 13:37:45 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Type undefined Newsgroups: comp.lang.javascript References: <9o4s0mFf1mU1@mid.uni-berlin.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 65 NNTP-Posting-Date: 23 Jan 2012 13:37:45 CET NNTP-Posting-Host: 38032762.newsspool2.arcor-online.net X-Trace: DXC=Q[i_I^_cFKBFJ3]dH>I?oEA9EHlD;3YcB4Fo<]lROoRA8kF On 23.01.2012 11:23, wrote Archos: >> Why I get an error about a type undefined when it has been defined in >> top of function Older10() ? >> >> === >> function person(name, age) {this.name=name; this.age=age;} >> >> function Older10(people) { >> var old = people[0]; >> >> for (var index = 1; index < 10; index++) { >> if (people[index] > old.age) { >> old = people[index]; // ERROR! TypeError: old is undefined >> } >> } >> return old; >> } >> >> function main() { >> var array = new Array(10); >> >> array[1] = new person("Paul", 23); >> array[2] = new person("Jim", 24); >> array[3] = new person("Sam", 84); >> array[4] = new person("Rob", 54); >> array[8] = new person("Karl", 19); >> >> var older = Older10(array); >> >> console.log("The older of the group is: " + older.name + "\n"); >> } >> === > > 'array[0]' in function 'main' is undefined. > Therefore 'people[0]' in function 'Older10' is undefined. Correct. However, the TypeError exception is _not_ thrown at the line that the OP marked, but one line *before*, when with `old.age' it is attempted to access the property named `age' of the value `undefined'. Whereas `undefined' is a primitive value (the sole value of the Undefined type) that cannot be wrapped into a temporary object [1], and therefore has no properties [2] (the same happens with `null', the sole value of the Null type [1, 3]). If that property access had not occurred, the value of `old' would have been overwritten with the value of `people[index]', and no exception would have been thrown (implementations of ECMAScript Editions 1 to 3, and 5.x use *loose typing* [4]). PointedEars ___________ [1] ECMAScript Language Specification, Edition 5.1 (ES 5.1), section 11.2.1, step 5, and therefore section 9.10 [2] ES 5.1, section 4.3.10 [3] ES 5.1, section 4.3.12 [4] ES 5.1, section 9 -- > If you get a bunch of authors […] that state the same "best practices" > in any programming language, then you can bet who is wrong or right... Not with javascript. Nonsense propagates like wildfire in this field. -- Richard Cornford, comp.lang.javascript, 2011-11-14