1.1.2 • Published 9 years ago

assign-props v1.1.2

Weekly downloads
7
License
MIT
Repository
github
Last release
9 years ago

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 the defineProperty() configurable value. Defaults to false.
  • enumerable - Sets the defineProperty() enumerable value. Defaults to true.

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"
1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago