Forum Moderators: open

Message Too Old, No Replies

Prototype Function Problems

         

vol7ron

5:11 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



I've worked with this before so I know it can work, I just don't know what I'm doing wrong this time.

The following returns undefined:

searchQueue = new queueClass();
alert(searchQueue.getLength);

function queueClass () {
this.queue = new Array();
// alert(queue.length); this returns 0
}
queueClass.prototype.getLength=function() { return queue.length; }

===============================================

Another thing I've tried:
searchQueue = new queueClass();
alert(searchQueue.getLength());

function queueClass () {
this.queue = new Array();
}
function getLength(){
with(this) {return queue.length;}
}
queueClass.prototype.getLength = getLength;

This returns an error saying getLength() is not a function. If I remove the () on the second line then I get the 'undefined' again, instead of the '0'.

Any help is greatly appreciated.

vol7ron

5:22 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



I should mention that these are in an external JavaScript file.

searchQueue = new queueClass(); etc..
is nested in a pageLoad() function.
the pageLoad function is called using a more elaborate version of:
window.onload = pageLoad();

And the window.onload function is at the top of the external script, following pageLoad().

Does that help at all?

vol7ron

5:33 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



Ahh nevermind!

My original way worked with the "this" keyword in front. I tried that before, but they weren't working this time because of another error in the script, which i guess halted the browser from continuing!

Thanks for anyone who tried to help.