0

I have a not so small image and I pre-loaded it like this.

//cache the poster
var img = document.createElement("img");
img.src = "http://lorempixel.com/300/200";
// to make sure the poster is loaded before the video player appears
img.addEventListener("load", function() {
    //img loaded, show video
});
delete img;

The above code, in strict mode gives me this error

Calling delete on an expression is not allowed in strict mode

I know why delete is not allowed in strict mode. My queries are

  1. Won't this take up memory/resource space?
  2. If so, is there a way to delete it?

P.S: Is someObject.img = createElm and then delete the property a better option?

naveen
  • 53,448
  • 46
  • 161
  • 251
  • 3
    If you aren't referencing it anywhere else, it will get garbage collected. You can't manually free memory in Javascript. The best you can do is ensure nothing else references a value then that value will be collected during the next possible cycle. The closest thing to what you're attempting that you could do would be to do `img = null`. – Mike Cluck Aug 11 '16 at 22:13
  • Possible duplicate of [Deleting Objects in JavaScript](http://stackoverflow.com/questions/742623/deleting-objects-in-javascript) – Mike Cluck Aug 11 '16 at 22:17
  • [IIFE](http://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript) - worth a mention. – Emissary Aug 11 '16 at 22:27
  • Consider moving any `delete someObj.img` or `img=null` statement into load event handler function to prevent a possible cancellation of HTTP requests to fetch the image if the image object gets garbage collected immediately. – traktor Aug 11 '16 at 23:32

1 Answers1

-1

try doing "remove" .I think img.remove() works

Sahithi
  • 83
  • 10