Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #10505
| Message-ID | <1934328.aqSPkdTlGX@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-01-23 13:37 +0100 |
| Subject | Re: Type undefined |
| Newsgroups | comp.lang.javascript |
| References | <d356459d-b8d8-4ba6-a0e7-42994e79e629@k6g2000vbz.googlegroups.com> <9o4s0mFf1mU1@mid.uni-berlin.de> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Jake Jarvis wrote:
> 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
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Type undefined Archos <raul.san@sent.com> - 2012-01-23 02:23 -0800
Re: Type undefined Jake Jarvis <pig_in_shoes@yahoo.com> - 2012-01-23 11:44 +0100
Re: Type undefined Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-23 13:37 +0100
Re: Type undefined "Tom de Neef" <tdeneef@qolor.nl> - 2012-01-23 19:44 +0100
csiph-web