I have a function which creates an object and I would like to delete it. I saw this post : Deleting Objects in JavaScript but I failed to create condition to delete it.
class setBackgroundColor {
constructor (element, options) {
this.element = element;
this.options = options;
this.element.style.backgroundColor = this.options;
}
}
function onWindowResize () {
let mobile = window.innerWidth < 800
if (mobile) {
new setBackgroundColor(document.querySelector('.my_container'), "red")
// var newObjet = new setBackgroundColor(document.querySelector('.my_container'), "red")
} else {
return
// delete newObjet
}
}
onWindowResize ();
window.addEventListener('resize', onWindowResize);
.my_container{
margin: 30px;
width: 100px;
height: 100px;
border: 1px solid black;
}
<div class="my_container"></div>