modify-property

For patching a property descriptor, including replacing getters/setters.

Usage no npm install needed!

<script type="module">
  import modifyProperty from 'https://cdn.skypack.dev/modify-property';
</script>

README

modify-property

A Node.js module which lets you patch the descriptor of an existing object property. This is primarily useful for swapping out getters or setters.

Installation

npm install modify-property --save

Usage

const obj = {}
Object.defineProperty(obj, 'name', {
  configurable: true, // <- Won't work if this isn't true
  enumerable: true,
  get () { return 'Bill' },
})

// Now we want to modify this property.

const modifyProperty = require('modify-property')

modifyProperty(obj, 'name', prop => { prop.get = () => 'Ben' })

obj.name // Ben