Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2007, 11:07 AM   PM User | #1
wr1472
New to the CF scene

 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
wr1472 is an unknown quantity at this point
Angry Having fundamental javascript OO problems

Hi

I'm new to javascript, and I'm trying to inmplement OO programming using javascript.

I have created an object called MhukWindow with various attributes, on a test page I am calling a function (downPressedScroller) on a button click, which in turn calls another function(downPressed).

within the downPressed function I try an access a object variable (currentV) which is initialised to zero. but this is coming back as undefined.

I can't fugure out why and this is driving me nuts! I need to understand such a fundamental problem if I am to learn javascript.

please help, my code is outlined below :-

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Untitled Document</title>
		
		<script type="text/javascript">
function MhukWindow()
{
    this.increment = 2;

    this.currentV = 0;

    this.scrollTimer = null;
    this.currentWindow = 'abc';
    //......more variables

    this.downPressedScroller = function(wndw)
    {
	currentWindow = wndw;
	alert('current window: ' + this.currentWindow); //ok
		
	this.scrollTimer = setInterval(this.downPressed,2000);    
}
		
    this.downPressed = function() 
    {			
             alert('current V: ' + this.currentV);	//comes back as undefined
    }
}
		var mhuk = new MhukWindow();

		alert('created instance');
		</script>
	</head>
	<body>

<input type="button" value="click me" onclick="mhuk.downPressedScroller('wndw');"/>
	</body>
</html>
wr1472 is offline   Reply With Quote
Old 02-07-2007, 11:40 AM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Code:
this.scrollTimer = setInterval(this.downPressed,2000);

This takes the function object stored in the downPressed property of the this object and calls it in 2 seconds. This is similar to if you did this:
Code:
var
    obj={
        fn:function(){
            return (this===obj);
        }},
    func=obj.fn;
alert(func());// ==> false
If you try that code, you'll see that the this object has to do with how the function is called, not with what object it was placed upon. The same function may be a member of many different objects...


To correct for this, you need to change your code a bit:
Code:
var
    o=this;
this.scrollTimer=setInterval(function(){o.downPressed()},2000);
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 02-07-2007, 11:55 AM   PM User | #3
wr1472
New to the CF scene

 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
wr1472 is an unknown quantity at this point
Thumbs up

liorean

I also came to the same conclusion that the this keyword was referring to the calling function. But hadn't come up with a fix yet!

Thanks for the prompt reply works a treat.
wr1472 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 AM.


Advertisement
Log in to turn off these ads.