/    Sign up×
Community /Pin to ProfileBookmark

How do I "reload" a JavaScript class?

Hi,
I’m trying to “reload” a class and I need help figuring out if I’m doing this correctly. I’m using MojoZoom as an example, but I consider this a general AJAX/JavaScript question.

MojoZoom is a simple class that adds zooming to an existing image. The problem, is that my image gallery works by changing the <img> “src” in Ajax so when you click a thumbnail for another image you see the new image but the zoomed part still uses the default one. This is probably because the MojoZoom class initialized only on page load, so I need to “reload” it.

The code for MojoZoom is 99% similar to this code I found online so you can see it: [url]http://code.google.com/p/feeling-luckier/source/browse/trunk/html/script/mojozoom.js?r=6[/url]

As you can see the original class is defined like this:

[CODE]var MojoZoom = (function() {
function init(){
}
})();
[/CODE]

I’ve edited mine to be more like:

[CODE]function MojoZoom(){
function init(){
}
this.init = init;
}[/CODE]

I made the changes because I was hoping it would allow me to do: var Mojo = MojoZoom(); when I wanted to “reload” it but that didn’t seem to work. Maybe because I need to dispose of the previous class somehow?

Thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 19.2011 — JavaScript has no classes. It works simply with [COLOR="Blue"]objects[/COLOR], functions and [COLOR="DarkGreen"]methods[/COLOR], thus you can not use the paradigm of class-based languages.

Try this to reinitialize the method:
<i>
</i>[COLOR="Blue"]MojoZoom[/COLOR].[COLOR="DarkGreen"]init()[/COLOR];
×

Success!

Help @auxone 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.17,
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,
)...