Type undefined

A

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");
}
===
 
T

Thomas 'PointedEars' Lahn

Jake said:
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
 
T

Tom de Neef

Archos said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top