Search code examples
javascriptfileobjectproperties

Is the JavaScri[t File object immutable?


In coffeescript I have this code, which is trying to extract GPS info from an image:

 EXIF.getData this, ->
                lat = EXIF.getTag(this, "GPSLatitude")
                long = EXIF.getTag(this, "GPSLongitude")
                alert(lat)
                alert(long)
                file.lat = lat
                file.lng = long
                console.log(file)

The alerts show me that the lat and long values are present, but it does not get stored in the the file object.

The console log output is this:

File { size: 95923, type: "image/jpeg", name: "pic1.jpg", path: "", lastModifiedDate: Date 2014-12-04T16:05:37.000Z, mozFullPath: "/home/me/Pictures/pic1.jpg" }

Is the File object somehow immutable or read-only as a JavaScript object? Am I missing something?

I know that Files are sensitive and all properties are read-only, but I am surprised one cannot change a File object to add own properties.


Solution

  • No, File objects are not immutable. You can always extend them with new properties.

    It seems like you have been fooled by your console. For some reason it didn't output the lat and lng properties, but you still will be able to access them on your object. Also try expanding ("inspecting") the logged object