/    Sign up×
Community /Pin to ProfileBookmark

JavaScript Objects

Hello everybody am a JavaScript beginner.

here is a code making rollovers
////////////////////////////////////////////////
[I][I][COLOR=”Blue”]window.onload=rolloverInit;

function rolloverInit()
{
for(var i=0; i<document.images.length; i++)
{
if(document.images[i].parentNode.tagName==”A”)
{
setUpRollover(document.images[i]);
}
}
}

function setUpRollover(currentImage)
{
currentImage.outImage = new Image();
currentImage.outImage.src = currentImage.src;
currentImage.onmouseout = rollOut;

currentImage.overImage = new Image();
var source = currentImage.src;
var sourceText = source.toString();
if(sourceText.indexOf(“png”)>0)
{
currentImage.overImage.src =”images/”+currentImage.id+”_on.png”;
}
else
{
currentImage.overImage.src =”images/”+currentImage.id+”_
on.gif”;
}

/*currentImage.overImage.src =”images/”+currentImage.id+”_on.gif”;*/
/*currentImage.overImage.src =”images/”+currentImage.id+”_
on.png”;*/
currentImage.onmouseover = rollOver;

currentImage.clickImage = new Image();
if(sourceText.indexOf(“png”)>0)
{
currentImage.clickImage.src =”images/”+currentImage.id+”_click.png”;
}
else
{
currentImage.clickImage.src =”images/”+currentImage.id+”_
on.gif”;
}
/*currentImage.clickImage.src = “images/”+currentImage.id+”_on.gif”;*/
/*currentImage.clickImage.src = “images/”+currentImage.id+”_
on.png”;*/
currentImage.onmousedown = rollClick;

currentImage.parentNode.childImage = currentImage;
currentImage.parentNode.onblur = rollOutParent;
currentImage.parentNode.onfocus = rollOverParent;

}

function rollOut()
{
this.src = this.outImage.src;
}

function rollOver()
{
this.src = this.overImage.src;
}

function rollClick()
{
this.src = this.clickImage.src;
}

function rollOutParent()
{
this.childImage.src = this.childImage.outImage.src;
}

function rollOverParent()
{
this.childImage.src = this.childImage.overImage.src;
}
[/COLOR][/I][/I]

////////////////////////////////////////////////

This code is working, but its obvious i am missing a term here related to OOP
because
I am unable to understand in this part of the code

///////
[I][I][COLOR=”Blue”]this.childImage.src = this.childImage.overImage.src;[/COLOR][/I][/I]
///////

Why I can NOT use
***[COLOR=”Blue”][I]this.overImage.src;[/I][/COLOR]***
instead of using
***[COLOR=”Blue”][I]this.childImage.overImage.src;[/I][/COLOR]***

————————————————————-

1-I do understand “if iam right, lol” that here
///[COLOR=”Blue”][I]currentImage.outImage = new Image();[/I][/COLOR]///
and here
///[I][COLOR=”Blue”]currentImage.overImage = new Image();[/COLOR][/I]///
and here
///[I][COLOR=”Blue”]currentImage.clickImage = new Image();[/COLOR][/I]///

we are creating an image object on the fly, to keep track and store the current image and new image src.

but
///[COLOR=”Blue”][I]currentImage.parentNode.childImage = currentImage;[/I][/COLOR]///
I am unable to see the point behind creating the childImage object
-is it really a brand new independent object created on teh fly liek before?
and if so
-what is is purpose here??

2-I am woundering if the src is all what i care for here, why i cant now just use the overimage and outimage instead of “[COLOR=”Blue”][I]this.childImage.overImage.src;[/I][/COLOR]

like that
///////
[COLOR=”Blue”][I]this.childImage.src = this.childImage.overImage.src;[/I][/COLOR]
///////

Why I can NOT use
***[COLOR=”Blue”][I]this.overImage.src;[/I][/COLOR]***
instead of using
***[COLOR=”Blue”][I]this.childImage.overImage.src;[/I][/COLOR]***

isnt it supposed that outimage and overimage are new objects have their own “src”???

OH my GOD
I am totally lost

in general, in this part of code
///
[COLOR=”Blue”][I]currentImage.parentNode.childImage = currentImage;
currentImage.parentNode.onblur = rollOutParent;
currentImage.parentNode.onfocus = rollOverParent;[/I]
[/COLOR]

///
-I dont understand how object childImage “if its an object at all” interact with my <A> object, what and how and why childImage is able to change the <A> object childenode Image object which is represented here as currentImage???
-after this
in the functions rollOutParent() and rollOverParent()
///
[COLOR=”Blue”][I]function rollOutParent()
{
this.childImage.src = this.childImage.outImage.src;
}

function rollOverParent()
{
this.childImage.src = this.childImage.overImage.src;
}[/I]
[/COLOR]
///
why cant i use OverImage.src to get the src for the targeted image to display instead of using this.childImage.overImage.src.

I know my questions looks very stupid, but the thing is as i said i have a problem with one of the object oriented programming concepts.

Any body can explain me this code and the ideas here and relations between objects please, I will really appreciate it.
Thanks in advance.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@justinbarneskinApr 05.2010 — "This" isn't the image

Rather, "this" is somewhere between the parent and the child

Solve for this
Copy linkTweet thisAlerts:
@tirnaApr 05.2010 — I'm sorry but my attention span isn't what it used to be when I'm looking at someone else's code that is more than about 10 lines long after which my head starts to hurt :o

But having a very quick look at your code, you might be able to do the whole lot in a few lines of css styling using pseudo classes for an element...eg :hover
Copy linkTweet thisAlerts:
@MiciauthorApr 05.2010 — "This" isn't the image

Rather, "this" is somewhere between the parent and the child

Solve for this[/QUOTE]


Dear justinbarneskin, Thanks for your reply.

1-I do understand that "this" keyword here is not the Image in this part of the code

///

function rollOutParent()

{

[COLOR="Blue"]this.childImage.src = this.childImage.outImage.src;[/COLOR]

}

///

"this" here is the <a> object because actually i am accessing an action listener for it in this part of the code

///

currentImage.parentNode.onblur = rollOutParent;

currentImage.parentNode.onfocus = rollOverParent;

///

-I can now understand why i cant use

///

this.overImage.src;

///

as in previous functions, because "this in previous functions was the Image object itself"

Excellent job Man,

I got it here Thanks a lot.
-----------------------------------------------------------------



2-second part of my question was that, how the cildImage object is interacting here, what is its purpose? and how it is able to modify "actually change" the <a> object child node Image object "currentImage".

it seems stupid question ? but its quiet simple

---///currentImage.parentNode.childImage = currentImage;///

here I have an object on the fly, a new Image object "childImage", that is Fine until now.

---///this.childImage.src = this.childImage.outImage.src;///

now how the childImage became able to change my parent "<A>" child "currentImage" src ??? how it "childImage" became displayable at all

is is because of the assignment here

///currentImage.parentNode.childImage = currentImage;///

???

Yes i assigned currentImage to the childImage, but based on my java programing concepts background each still independent objects refrences representing two different objects

---->and so based on that here is these functions rollOutParent(), rollOverParent()

am supposed to say something like

***this.currentImage.src = this.childImage.src*** or

***this.childImage.src = this.childImage.outImage.src;***
------------


Man am confused, how with this simple line of code

currentImage.parentNode.childImage = currentImage;

childImage became able to define the currentImage ?
Copy linkTweet thisAlerts:
@MiciauthorApr 05.2010 — I'm sorry but my attention span isn't what it used to be when I'm looking at someone else's code that is more than about 10 lines long after which my head starts to hurt :o

But having a very quick look at your code, you might be able to do the whole lot in a few lines of css styling using pseudo classes for an element...eg :hover[/QUOTE]


woops...sorry for hurting your head mate ?,

and yea I know I can do the whole lot in fewer lines, I can and I did the whole task in sometimes fewer sometimes bigger lines also in JavaScript and it worked.

This code here is from one tutorial "with some modifications from my side", I just want to understand it, because I am so ashamed from myself when I cant, because the reason I cant understand it is am totally unaware of the Objects concepts or am just so poor minded ?.

Thanks for your care anyway , and I appreciate the try to help.

Good Luck.
×

Success!

Help @Mici spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...