Click to See Complete Forum and Search --> : How does this thing work?


eewald
05-20-2003, 09:49 AM
I can't figure out how this works.. but it does.

function toString() {
var t = '';
for (var i in this) {

if (typeof(this[i]=='function')) continue;
t += i+' = '+this[i]+'\n';
}
return t;
}

BrowserInfo.prototype.toString = toString;
var oBrowser = new BrowserInfo();

*******************************************

Heres what I can figure out. So theres a function called browser info.. and the new toString() function overwrites the default toString function. This new toString function just outputs the entire function in a string. The lines that I dont understand are

for (var i in this) {

if (typeof(this[i]=='function')) continue;
t += i+' = '+this[i]+'\n';
}

Doesnt var i have to be defined somewhere.. and how can you check whether its equal to 'function'. Why would they use typeof(this[i]=='function') instead of just (this[i]=='function').

Javascript shouldnt be this confusing right? :)

Gollum
05-20-2003, 10:10 AM
Methinks the line that says...

if (typeof(this[i]=='function')) continue;

was proably intended to be...

if (typeof(this[i])=='function') continue;

then the output of BrowserInfo.toString() will be all the non-function values of the instance.

typeof(this[i]=='function') will probably evaluate to 'Boolean'

regarding the variable i, it does get defined. Have a look at the for statement...

for (var i in this)

eewald
05-20-2003, 12:34 PM
The fog is clearing.

Thanks!