Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including unwritable with all npm packages installed. Try it out:

var unwritable = require("unwritable")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

unwritable v1.1.0

Convenience for working with `writable: false` object properties.

unwritable

Convenience for working with writable: false object properties.

Read about the writable attribute here.

install

# npm install unwritable

example

const { unwritable, writable, write } = require('unwritable')

const obj = { foo: 123 }

unwritable(obj, 'foo')

obj.foo = 456 // does nothing
obj.foo // => 123

write(obj, 'foo', 456)
obj.foo // => 456

writable(obj, 'foo')
obj.foo = 123
obj.foo // => 123
const unwritable = require('unwritable')

const obj = { foo: 123 }

unwritable(obj, 'foo')

obj.foo = 456 // does nothing
obj.foo // => 123

unwritable.write(obj, 'foo', 456)
obj.foo // => 456

unwritable.writable(obj, 'foo')
obj.foo = 123
obj.foo // => 123

api

These functions will throw if given a property that has an accessor (get/set). This gives you added confidence that your code is doing what you expect.

unwritable(obj, prop)

Make prop of obj unwritable. It will assign prop to object with a value of undefined if obj does not already have prop.

writable(obj, prop)

Make prop of obj writable. It will assign prop to object with a value of undefined if obj does not already have prop.

write(obj, prop, value)

Assigns value to prop of obj. It is essentially obj[prop] = value, but works on properties whether they are writable or not.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free