Hice un script que crea una etiqueta HTML usando JavaScript. Declaré una variable llamada etiqueta, sabiendo específicamente que no puedes delete() let y var variables, pero no la eliminará automáticamente en mi código:
function(tagType, attr1 = null, value1 = null, attr2 = null, value2 = null, attr3 = null, value3 = null, attr4 = null, value4 = null, attr5 = null, value5 = null, attr6 = null, value6 = null, attr7 = null, value7 = null, attr8 = null, value8 = null, attr9 = null, value9 = null, attr10 = null, value10 = null) { tag = document.createElement(tagType); if (attr1 != null) { tag.setAttribute(attr1, value1) }; if (attr2 != null) { tag.setAttribute(attr2, value2) }; if (attr3 != null) { tag.setAttribute(attr3, value3) }; if (attr4 != null) { tag.setAttribute(attr4, value4) }; if (attr5 != null) { tag.setAttribute(attr5, value5) }; if (attr6 != null) { tag.setAttribute(attr6, value6) }; if (attr7 != null) { tag.setAttribute(attr7, value7) }; if (attr8 != null) { tag.setAttribute(attr8, value8) }; if (attr9 != null) { tag.setAttribute(attr9, value9) }; if (attr10 != null) { tag.setAttribute(attr10, value10) }; document.getElementsByTagName('head')[0].appendChild(tag); if (tag) { delete(this.tag); } }, Al final del código, dije delete(tag) y no funcionó. Entonces traté de verificar si la variable con nombre tag estaba allí. if(tag) { delete(tag) } .
¿Por qué no elimina automáticamente la variable? Si solo escribo delete(tag) en la consola, lo eliminará.
¡Gracias por tu ayuda!