Question:

Javascript: Objects are replicated when pushed into array?

by Guest21475  |  earlier

0 LIKES UnLike

Consider the following code:

obakaSan = new Object();

obakaSan.age = 14;

hairetsu = [];

hairetsu.push(obakaSan);

ahoSan = obakaSan;

alert( obakaSan.age ", " "ahoSan.age ", " hairetsu[0].age );

This one should give the same number for each object ( giving a message box "14, 14, 14"). Now, try this:

delete hairetsu[0];

Thus doing alert(hairetsu[0].age) will cause an error, but alert(obakaSan.age) still produces output. But,

delete ahoSan;

will also effectively delete obakaSan as well, causing error when doing alert(obakaSan.age). Does this imply that deleting object from array only removes the reference to the object and not the object itself?

 Tags:

   Report

1 ANSWERS


  1. Not trivial:

    In MSIE, if no variables are tracking an object, the JavaScript interpreter will ignore this object.

    Different from browser to browser.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.