Where is the property going?

F

flaxeater

I have made this objec because I'm trying to encapsulate the namespaces
so that it'll work for multiple images on one page and I'd like to be
able give this away to people. However when I run this the
this.fadeimages property get's lost I don't know why. here's the code

<html><head><title>EXample</title><script type="text/javascript">
function Fader() {
this.fadeimages=new Array()
}

Fader.prototype.getfadeimages=getfadeimages;
function getfadeimages() {
alert('getfadeimgaes');
return this.fadeimages;
}//end getfadeimges()
Fader.prototype.setfadeimages=setfadeimages;
function setfadeimages(arry) {
alert('setfadeimages');
this.fadeimages=arry;
}//end setfadeimgaes
Fader.prototype.go=go;
function go() {
alert('GO');alert(self.getfadeimages()[0][0]);
}

</script></head><body><script type="text/javascript">
var fader=new Fader();
imagelist=new Array();
imagelist[0]=["stock/table1.jpg", "", ""] //plain image syntax
imagelist[1]=["stock/cake1.jpg", "http://www.google.com", ""] //image
with link syntax
imagelist[2]=["stock/eggs1.jpg", "http://www.javascriptkit.com",
"_new"] //image with link and target syntax
fader.setfadeimages(imagelist);
alert('fadeimages[2][2] ' + fader.fadeimages[2][2]);
fader.go()
</script></body></html>
 
L

Lee

(e-mail address removed) said:
I have made this objec because I'm trying to encapsulate the namespaces
so that it'll work for multiple images on one page and I'd like to be
able give this away to people. However when I run this the
this.fadeimages property get's lost I don't know why. here's the code

<html><head><title>EXample</title><script type="text/javascript">
function Fader() {
this.fadeimages=new Array()
}

Fader.prototype.getfadeimages=getfadeimages;
function getfadeimages() {
alert('getfadeimgaes');
return this.fadeimages;
}//end getfadeimges()
Fader.prototype.setfadeimages=setfadeimages;
function setfadeimages(arry) {
alert('setfadeimages');
this.fadeimages=arry;
}//end setfadeimgaes
Fader.prototype.go=go;
function go() {
alert('GO');alert(self.getfadeimages()[0][0]);
}

</script></head><body><script type="text/javascript">
var fader=new Fader();
imagelist=new Array();
imagelist[0]=["stock/table1.jpg", "", ""] //plain image syntax
imagelist[1]=["stock/cake1.jpg", "http://www.google.com", ""] //image
with link syntax
imagelist[2]=["stock/eggs1.jpg", "http://www.javascriptkit.com",
"_new"] //image with link and target syntax
fader.setfadeimages(imagelist);
alert('fadeimages[2][2] ' + fader.fadeimages[2][2]);
fader.go()
</script></body></html>

You've got a couple of problems. The trivial one, correcting which
may make your code *appear* to work, is that your "go()" method
refers to self.getfadeimages() instead of this.getfadeimages().

The more serious problem is that you don't seem to realize what
happens when you assign one array reference to another.

this.fadeimages=arry

discards the reference to the empty array that your Fader()
constructor created and replaces it with a reference to the
array that you passed in as an argument. At this point, the
reference in your Fader object points to the global imagelist
array. None of the elements have been copied. If you now
make changes to imagelist, those changes will be reflected
when you invoke fader.go();

To copy an array, use one of the Array methods, such as:

this.fadeimages=arry.slice(0);
 
F

flaxeater

To copy an array, use one of the Array methods, such as:

this.fadeimages=arry.slice(0);

I've done what you have suggested. The self was somethign I have since
changed back and forgot to change back. I get the result I expect from
inside go but when I call getfadeimages from any other function it's
gone. and I've enumerated through the object properties and printed
them and none of my object properties are showing. It seems like
this.fadeimages disapears. If you think it would be helpful I'll post
the complete code that I have so far.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top