assign-props
v1.1.2
Published
Quickly define immutable properties on objects.
Downloads
9
Readme
Assign Props
Define immutable values on JavaScript objects.
$ npm install assign-props
var assignProps = require("assign-props");
var obj = {};
assignProps(obj, "foo", "bar");
obj.foo = 12345;
obj.foo; // → "bar"
Usage
assignProps(obj, key, value [, options ])
Define an immutable key
on some JavaScript object, obj
. This is sugar for Object.defineProperty()
.
If value
is a function, it is considered a dynamic value and an accessor descriptor will be defined.
assignProps(obj, "four", function() {
return 2 + 2;
});
obj.four; // → 4
If value
is any other than a function, or options.forceStatic
is true, value
will be defined using a data descriptor.
assignProps(obj, "hello", "world");
obj.hello; // → "world"
Here are the available options:
forceStatic
- Forces the value to applied as a static value. Useful if you absolutely need a function defined with a data descriptor.configurable
- Sets thedefineProperty()
configurable value. Defaults tofalse
.enumerable
- Sets thedefineProperty()
enumerable value. Defaults totrue
.
assignProps(obj, props [, options ])
Similar to above, this assigns multiple values to a JavaScript object, obj
. Pass an object of keys mapped to values and they will be applied in the same fashion.
assignProps(obj, {
name: "World",
greeting: function() {
return "Hello " + this.name;
}
});
obj.greeting; // → "Hello World"