...

View Full Version : object wont alert in a function that I call from a function



andrew1234
07-18-2009, 07:54 PM
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



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);

}

celsoendo
07-18-2009, 08:01 PM
Because arrImages is not declared anywhere?!

andrew1234
07-18-2009, 08:06 PM
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?

Amphiluke
07-18-2009, 08:07 PM
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().

Philip M
07-18-2009, 08:13 PM
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.

andrew1234
07-18-2009, 08:15 PM
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
07-18-2009, 08:16 PM
thanks for the help

Philip M
07-18-2009, 08:19 PM
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/tutorials/javascript/global_local_variables_scope_javascript.php3

ckeyrouz
07-18-2009, 08:20 PM
The variable arr that you have declared this way:

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


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
07-18-2009, 08:20 PM
sorry for the reply I did not see the other reply.

andrew1234
07-18-2009, 10:52 PM
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


function test(){

var calcAnswer;
//Can it be done?


}

ckeyrouz
07-18-2009, 11:34 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum