# patch-obj-prop

> Patch an object instance's property at runtime with accessors.

Latest version **1.0.0** (published 2022-06-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install patch-obj-prop
pnpm add patch-obj-prop
yarn add patch-obj-prop
bun add patch-obj-prop
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-06-08 |
| First published | 2022-06-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Katsuyuki Omuro |
| Maintainers | harmony7 |
| Keywords | object, patching, monkeypatch |

## Links

- npm: https://www.npmjs.com/package/patch-obj-prop
- Repository: https://github.com/harmony7/js-patch-obj-prop
- Homepage: https://github.com/harmony7/js-patch-obj-prop#readme
- Issues: https://github.com/harmony7/js-patch-obj-prop/issues
- npm.io page: https://npm.io/package/patch-obj-prop

## Recent versions

- 1.0.0 (latest) — 2022-06-08

## README

# patch-obj-prop

Patch an object instance's property at runtime with a getter and/or setter.

```javascript
import { monkeyPatchProp } from "patch-obj-prop";

const obj = {
  foo: 'bar',
};

// Replace with a getter
monkeyPatchProp(obj, 'foo', {
  get() {
    return 'baz';
  }
});

console.log(obj.foo); // baz

// Replace with a getter, and get a reference to the original getter
// Note that the "original" getter at this point is the one that was
// set in the above call.
let called = false;
monkeyPatchProp(obj, 'foo', {
  get(origGetter) {
    called = true;
    return origGetter();
  }
});

console.log(obj.foo); // baz
console.log(called); // true
```

Setters work in a very similar way:


```javascript
import { monkeyPatchProp } from "patch-obj-prop";

const obj = {
  foo: 'bar',
};

// Replace with a setter
let x = null;
monkeyPatchProp(obj, 'foo', {
  set(value) {
    x = value;
  }
});

obj.foo = 'baz';
console.log(x); // baz
console.log(obj.foo); // bar

// Replace with a setter, and get a reference to the original setter
// Note that the "original" setter at this point is the one that was
// set in the above call.
let called = false;
monkeyPatchProp(obj, 'foo', {
  set(value, origSetter) {
    called = true;
    return origSetter(value);
  }
});

obj.foo = 'baz';
console.log(obj.foo); // bar
console.log(called); // true
```

### License

[MIT](./LICENSE.md)

---
_Source: https://npm.io/package/patch-obj-prop · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
