Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #10503 > unrolled thread

Type undefined

Started byArchos <raul.san@sent.com>
First post2012-01-23 02:23 -0800
Last post2012-01-23 19:44 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#10503 — Type undefined

FromArchos <raul.san@sent.com>
Date2012-01-23 02:23 -0800
SubjectType undefined
Message-ID<d356459d-b8d8-4ba6-a0e7-42994e79e629@k6g2000vbz.googlegroups.com>
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");
}
===

[toc] | [next] | [standalone]


#10504

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2012-01-23 11:44 +0100
Message-ID<9o4s0mFf1mU1@mid.uni-berlin.de>
In reply to#10503
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.

-- 
Jake Jarvis

[toc] | [prev] | [next] | [standalone]


#10505

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-01-23 13:37 +0100
Message-ID<1934328.aqSPkdTlGX@PointedEars.de>
In reply to#10504
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

[toc] | [prev] | [next] | [standalone]


#10520

From"Tom de Neef" <tdeneef@qolor.nl>
Date2012-01-23 19:44 +0100
Message-ID<4f1daaa3$0$6972$e4fe514c@news2.news.xs4all.nl>
In reply to#10503
"Archos" <raul.san@sent.com> schreef in bericht 
news:d356459d-b8d8-4ba6-a0e7-42994e79e629@k6g2000vbz.googlegroups.com...
> 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) {

I guess you intended to code
   if (people[index].age > old.age) {

Tom 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web