By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,259 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,259 IT Pros & Developers. It's quick & easy.

assigning a new object to a variable deletes the previous and all its event bindings?

Mike Kypriotis
P: 37
I have loop where at each loop I need a new image object so if I put a
Expand|Select|Wrap|Line Numbers
  1. var img=new Image();
  2. addEvent(img,'load',function(){validBannerImage(iNum,oInputData);});
  3. addEvent(img,'error',function(){invalidBannerImage(iNum,oInputData);});
  4.  
at each loop when I assign the new object the previous get deleted as well as its bindings to functions (for the onload and onerror events)

P.S Tried to define the above before the loop but needed the parameters (iNum,oInputData) to call the fuctions and they are calculated inside the loop
Dec 29 '11 #1
Share this Question
Share on Google+
5 Replies


Dormilich
Expert Mod 5K+
P: 8,639
P.S Tried to define the above before the loop but needed the parameters (iNum,oInputData) to call the fuctions and they are calculated inside the loop
that’s the problem. you are trapped by a Closure. you will therefore get the values of the parameters in their last loop cycle.

to counter that, break the Closure through a "Counter-Closure"
Expand|Select|Wrap|Line Numbers
  1. for (…) {
  2.     var param1 = …, param2 = …;
  3.     (function (p1, p2) {
  4.         addEvent(img, 'load', function() {
  5.             validBannerImage(p1, p2);
  6.         });
  7.     })(param1, param2);
  8. }
Dec 30 '11 #2

Mike Kypriotis
P: 37
hmmm...didn't quite understood you. Think is I need an image objeect in each loop so Imove my 3 statements
Expand|Select|Wrap|Line Numbers
  1. var img=new Image();
  2. addEvent(img,'load',function(){validBannerImage(iNum,oInputData);});
  3. addEvent(img,'error',function(){invalidBannerImage(iNum,oInputData);});
  4.  
inside the loop and all is ok thing is when i redifine the img as new image() the old and its event handlers (which are no longer in use + I do not use them anymore) get automatically deleted ? cause the loop runs forever (its an image scroller) so the amount of useless image objects will become a problem at some time if they are not deleted
Dec 31 '11 #3

Dormilich
Expert Mod 5K+
P: 8,639
cause the loop runs forever
you mean it’s an infinite loop?
Jan 1 '12 #4

Mike Kypriotis
P: 37
yes its is an infinite loop so "assigning a new object to a variable deletes the previous and all its event bindings?" would be very helpful if the answer was yes (I reassign a new object to the same variable in each loop )
Jan 1 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
yes its is an infinite loop
that is a no-go in JavaScript, because it is event-driven

"assigning a new object to a variable deletes the previous and all its event bindings?"
let’s put it this way: if you delete the last reference to an object (objects are always passed by reference) JS deletes the object with all its associated data. assigning a new object to a variable holding a reference to another object will delete that previous reference.
Jan 1 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.