The problem of referencing objects in JS

problem description

newcomer, I encountered a variable reference problem when learning to delete option from the select list, as follows:
1. Use option [index] = null, you can achieve the purpose of deletion;
2. Assign option [index] to a variable, and then set the variable to invalid null, for example:

var a = option[index];
a = null;

after thinking, I have realized that an actually refers to the address of option [index]. Setting a = null, only changes the reference address of a, and option [index] has not actually changed. However, I can"t figure out the following question. The code is posted below.

Code

<form action="" name="theForm">
    <select name="theDay">
        <option value="1">1</option>
    </select>
</form>
<script>
    var option = document.theForm.theDay.options[0];
    document.theForm.theDay.options[0] = null;
    console.log(document.theForm.theDay.options[0]); // undefined 
    console.log(option); //  <option value="1">1</option>
</script>

and I thought the output would be

console.log(document.theForm.theDay.options[0]); // null 
console.log(option); //  null

I haven"t figured it out for a long time. I hope God will give me some advice

.
Mar.21,2022

you can delete it with option [index] = null. is caused by the browser's garbage collection mechanism, which only deletes the referenced objects in memory.

var a = {};
a = null

here only empty objects are recycled and deleted, but the variable a still exists.

if option is an array, you will find that option [index] becomes null, which is what you think is the output below. But here your option is not an array, but a collection of array objects like HTMLOptionsCollection (option, which inherits from HTMLCollection)

when you use option to receive theForm.theDay.options [0] , option references this option object. When you leave theForm.theDay.options [0] empty, the length of the class array object changes to zero, and when you visit it, it becomes undefined, instead of the null you think it is. At the same time, because there is also an option variable pointing to the option object, the option object is not recycled, and you can still print the dom object when you print the dom object.


first of all, document.theForm.theDay.options [0] = null is to change the direction of options [0] memory, but the direction of option remains unchanged, it still points to the instance (instance), of < option > , so the second console prints out the instance of < option > .

then document.theForm.theDay.options [0] = null removes the unique < option > from the document, so when you execute document.theForm.theDay.options [0] to query, you will not find it and return undefined.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a675-345da.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a675-345da.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?