1

Is there a way to detect object property deletion?

meager
  • 209,754
  • 38
  • 307
  • 315
geeko
  • 2,278
  • 1
  • 27
  • 44
  • 1
    In 2000, the far future you will be able to use JavaScript Proxy objects: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Proxy – biziclop Jul 03 '12 at 06:21

4 Answers4

1

Yes, it is possible. Just as you can use setters/getters for properties, you can use a "deleter" function for object properties.

function myFunc() {}
myFunc.prototype = {
    constructor: myFunc,

    deleteProp: function( prop ) {
        delete this[ prop ];
    }
};

In the deleteProp method, you can do anything before and after.

Florian Margaine
  • 50,873
  • 14
  • 87
  • 110
  • I'm curious why you replace the default prototype object. Why not just add the new method, e.g. `myFunc.prototype.deleteProp = function(){...}`? The you don't have to reset the constructor property. – RobG Jul 04 '12 at 10:07
  • @RobG just an habit :-) But yeah, for just one property, using `myFunc.prototype.deleteProp` is enough – Florian Margaine Jul 04 '12 at 12:05
  • 1
    This isn't really "detecting" deletes, ie. detecting calls to "delete obj.prop;" -- instead, it's providing a wrapper which can be called instead. This can be helpful, but is not exactly what was asked for. – Venryx Jun 10 '17 at 08:45
  • I rarely downvote something that may still be useful, but I agree with Venryx. This is far from a solution one would expect for "Detect[ing] Object Property Deletion". – Cody Apr 23 '20 at 21:26
1

You can check whether a property exists on the object itself using hasOwnProperty or perhaps propertyIsEnumerable, but that won't, by itself, tell you if the property once existed and has subsequently been deleted. You can test whether own and inherited properties have a value using:

if (typeof obj.prop != 'undefined') 

but that doesn't tell you whether the property exists or not, only whehter it has been assigned a value other than undefined. Note that if the property has been created but not assigned a property, it will still return undefined. For most purposes, that's sufficient.

What do you want to use this for? In ES5 you can create non–deletable properties using Object.create and Object.defineProperty, but support is lacking in some browsers so not suitable for the general web.

RobG
  • 124,520
  • 28
  • 153
  • 188
0

You can't override the delete operator so, apart checking regularly if a property is still here, I don't think you can do that.

EDIT: this was the right answer in 2012. Now it's obviously easy to do with proxies

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
0

What about ES6 Proxy? (MDN for references, stackoverflow for simplicity)

The docs clearly states that you can add a "trap" for "deleteProperty"... or in simple words - "listen" to whenever a property is deleted from a specific object (MDN to the rescue)

const myObject = { say: 'hello', to: 'proxy' }
const proxyHandler = {
  deleteProperty(target, prop) {
    if (prop in target) {
      delete target[prop]
      console.log(`property removed: ${prop}`)
    }
  }
}
const proxy = new Proxy(myObject, proxyHandler)
delete proxy.to
console.log(myObject)

Also, please check Can I Use for browsers comparability

Cheers!

ymz
  • 4,053
  • 1
  • 14
  • 31