Package Exports
- static-props
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (static-props) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
static-props
defines static object attributes using Object.defineProperties
Table Of Contents:
Installation
With npm do
npm install static-props --save
Usage
Let's create a classic Point2d class and add constant attributes.
'use strict'
var staticProps = require('static-props')
class Point2d {
constructor (x, y, label) {
// Suppose you have few static attributes in your class.
const color = 'red'
// Create a getter.
const norm = () => x * x + y * y
// Add constant attributes quickly.
staticProps(this)({label, color, norm})
// Add enumerable attributes.
var enumerable = true
staticProps(this)({x, y}, enumerable)
}
}
After instantiating the class, we can check that its props cannot be changed.
var p = new Point2d(1, 2)
// Trying to modify a static prop will throw as you expect.
p.label = 'B'
// TypeError: Cannot assign to read only property 'label' of #<Point2d>
Props label and color are values, while norm is a getter.
console.log(p.label) // 'A'
console.log(p.color) // 'red'
console.log(p.norm) // 5 = 1 * 1 + 2 * 2
Attributes x
, y
were configured to be enumerable
console.log(p) // Point2d { x: 1, y: 2 }
If you want to add a static attribute class, you can also do
staticProps(Point2d)({ dim: 2 })
so you can access it with
console.log(Point2d.dim) // 2
Annotated source
API is staticProps(obj)(props[, enumerable])
/**
* @param {Object} obj
* @returns {Function}
*/
function staticProps (obj) {
/**
* @param {Object} props
* @param {Boolean} [enumerable]
*/
return function (props, enumerable) {
Add every prop to obj as not writable nor configurable, i.e. static
var staticProps = {}
for (var propName in props) {
var staticProp = {
configurable: false,
enumerable: enumerable
}
If prop is a function use it as a getter, otherwise as a value
var prop = props[propName]
if (typeof prop === 'function') staticProp.get = prop
else {
staticProp.value = prop
staticProp.writable = false
}
staticProps[propName] = staticProp
}
Finally, apply the Object.defineProperties function
Object.defineProperties(obj, staticProps)
}
}
Export function
module.exports = staticProps