Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Objects and variable scope
Old 12-19-2006, 04:42 PM Objects and variable scope
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
Hey, this is a very simplified version of a Dashboard script I'm writing. I'm new to using objects, so I'm missing something here. I'm trying to figure out why inside download.start() the object variable "this.size" calls the right number, but in download.done(), it's coming up as undefined. Any help?

Code:
var download = {
	size : null,
	
	start : function(startSize) {
		this.size = startSize;
		setTimeout(this.done, 2000);
		alert("The Size is: " + this.size);
	},
	
	done : function() {
		alert("Now, the Size is: " + this.size);
	}
}

window.onload = function() {
	download.start(10);
}

EDIT - figured it out : setTimeout("download.done()", 2000); b/c when called the "this" is referring the window.

Last edited by funkdaddu; 12-19-2006 at 05:38 PM..
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
 
Register now for full access!
Old 01-01-2007, 04:02 PM Re: Objects and variable scope
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Very interesting problem. I honestly can't explain the behavior but, changing the inner function 'done' to
Code:
    done : function() {
        alert("Now, the Size is: " + download.size);
    }
will make it do what is expected.

If you don't mind, I'd like to post this on the 'comp.lang.javascript' newsgroup. There are some folks there that know js inside and out.
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 01-04-2007, 02:08 AM Re: Objects and variable scope
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Ok, here is the answer.
the value of 'this' changes in the second method call.
When you do this:
Code:
 setTimeout(this.done, 2000);
the 'done' function is being called as part of the 'window' object instead of the download object. In this context, 'this' refers to the 'window' object.

However, if you change the timeout to this:
HTML Code:
 setTimeout("download.done()", 2000);
Then, the 'this' object in the 'done' function will refer to the download object.

I hope that makes sense
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 01-16-2007, 09:59 AM Re: Objects and variable scope
Skilled Talker

Posts: 96
Name: Tudor Barbu
Trades: 0
You didn't create an object to refer to with "this". Those are "static methods" if you want to make an analogy with traditional OOP.
__________________

Please login or register to view this content. Registration is FREE
Tudor.b is offline
Reply With Quote
View Public Profile Visit Tudor.b's homepage!
 
Reply     « Reply to Objects and variable scope
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13489 seconds with 12 queries