how to use the __definesetter__ function from fs-extra in javascript

__definesetter__ is a private function from fs-extra that allows defining custom setters on an object. Here is an example of how to use it:

index.tsx
const fs = require('fs-extra')

const obj = {
  _value: 'default',

  get value() {
    return this._value
  },

  set value(newValue) {
    this._value = newValue
  }
}

fs.__definesetter__(obj, 'value', (newValue) => {
  console.log(`New value: ${newValue}`)
})

obj.value = 'some value' // logs "New value: some value"
322 chars
20 lines

In this example, we define an object with a private _value property and a getter and a setter for the value property. We then use the __definesetter__ function to define a custom setter that logs the new value to the console. When we set the value property to a new value, the custom setter gets called and logs the new value.

gistlibby LogSnag