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 07-18-2009, 06:54 PM   PM User | #1
andrew1234
Regular Coder

 
Join Date: Oct 2002
Posts: 147
Thanks: 31
Thanked 2 Times in 2 Posts
andrew1234 is an unknown quantity at this point
object wont alert in a function that I call from a function

Hi

Thanks for the help. I'm trying to get my head around this stuff.

Anyway here is the question.

Why won't the alert(arr[1].src); work in the showMe(); function

I don't get an error saying what I'm doing wrong

any ideas?

Thanks

Andrew


Code:
firstStep();

function firstStep(){

	var arr = new Array()
	
	for(var i=1;i<=6;i++){
		
			arr[i] = new Image();
			arr[i].src = "images/no"+i+".jpg";
					
			alert(arr[i].src);
		}
		
	showMe();	
		
}

function showMe(){


alert(arr[1].src);

}

Last edited by andrew1234; 07-18-2009 at 07:03 PM..
andrew1234 is offline   Reply With Quote
Old 07-18-2009, 07:01 PM   PM User | #2
celsoendo
New Coder

 
Join Date: Jun 2009
Posts: 18
Thanks: 0
Thanked 2 Times in 2 Posts
celsoendo is an unknown quantity at this point
Because arrImages is not declared anywhere?!
celsoendo is offline   Reply With Quote
Old 07-18-2009, 07:06 PM   PM User | #3
andrew1234
Regular Coder

 
Join Date: Oct 2002
Posts: 147
Thanks: 31
Thanked 2 Times in 2 Posts
andrew1234 is an unknown quantity at this point
Hi

Thanks for the reply

Sorry about that I thought I fixed that in the first post.

Still the alert(arr[1].src); work in the showMe(); function

any ideas?
andrew1234 is offline   Reply With Quote
Old 07-18-2009, 07:07 PM   PM User | #4
Amphiluke
Regular Coder

 
Amphiluke's Avatar
 
Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
Amphiluke is on a distinguished road
Quote:
Why won't the alert(arr[1].src); work in the showMe(); function
You have declared the arr variable as a local variable and that is why you cannot refer it anywhere outside the function firstStep().
__________________
I am still learning English
Amphiluke is offline   Reply With Quote
Users who have thanked Amphiluke for this post:
andrew1234 (07-18-2009)
Old 07-18-2009, 07:13 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,101
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Because as Amphiluke says you have defined the array arr as local to the function firstStep().

Change to:-

var arr = new Array(); // declare the array outside the function to make it global
firstStep();
function firstStep() {

Quizmaster: Which Boris recently became Mayor of London?
Contestant: Yeltsin.
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
andrew1234 (07-18-2009)
Old 07-18-2009, 07:15 PM   PM User | #6
andrew1234
Regular Coder

 
Join Date: Oct 2002
Posts: 147
Thanks: 31
Thanked 2 Times in 2 Posts
andrew1234 is an unknown quantity at this point
thanks ... now it works ...yeee haaaaaaa

so am I understanding this correctly.


if I declare the var inside the function it is only global in that function?
andrew1234 is offline   Reply With Quote
Old 07-18-2009, 07:16 PM   PM User | #7
andrew1234
Regular Coder

 
Join Date: Oct 2002
Posts: 147
Thanks: 31
Thanked 2 Times in 2 Posts
andrew1234 is an unknown quantity at this point
thanks for the help
andrew1234 is offline   Reply With Quote
Old 07-18-2009, 07:19 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,101
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by andrew1234 View Post
if I declare the var inside the function it is only global in that function?
Oh dear! No, if you declare variable with var within a function, the variable is local to that function. If you declare a variable (with or without var) outside all functions, then that variable is global, meaning accessible to all functions in the program. This is called the scope of the variable. OK?

For more info see:- http://www.webdevelopersnotes.com/tu...avascript.php3

Last edited by Philip M; 07-18-2009 at 07:23 PM..
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
andrew1234 (07-18-2009)
Old 07-18-2009, 07:20 PM   PM User | #9
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
The variable arr that you have declared this way:
Code:
var arr = new Array()
is a private variable inside the function firstStep() so the other method showMe will not be able to access it.

it should be declared as global variable outside the scope of the function like this
Code:
var arr = new Array();
firstStep();

function firstStep(){

	
	
	for(var i=1;i<=6;i++){
		
			arr[i] = new Image();
			arr[i].src = "images/no"+i+".jpg";
					
			alert(arr[i].src);
		}
		
	showMe();	
		
}

function showMe(){


alert(arr[1].src);

}
and by the way use semicolons at the end of each javascript command.
ckeyrouz is offline   Reply With Quote
Users who have thanked ckeyrouz for this post:
andrew1234 (07-18-2009)
Old 07-18-2009, 07:20 PM   PM User | #10
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
sorry for the reply I did not see the other reply.
ckeyrouz is offline   Reply With Quote
Old 07-18-2009, 09:52 PM   PM User | #11
andrew1234
Regular Coder

 
Join Date: Oct 2002
Posts: 147
Thanks: 31
Thanked 2 Times in 2 Posts
andrew1234 is an unknown quantity at this point
thanks for the help..everyone

Heres one more question.

Is it possible to make a variable global so that all other functions can use it.But it is declared inside the function test();

eg

Code:
function test(){

var calcAnswer;
//Can it be done?


}
andrew1234 is offline   Reply With Quote
Old 07-18-2009, 10:34 PM   PM User | #12
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
Never tried it before but you might try this:

function test()
{
var tempVar = "tmp";
this.calcAnswer = tempVar ;

}

I never tried such thing but I think it works.
Did not try it.
And if it works, nake sure that you assign to it the last value.
ckeyrouz 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 07:29 AM.


Advertisement
Log in to turn off these ads.