https://github.com/fibo/static-props
defines static object attributes using `Object.defineProperties`
https://github.com/fibo/static-props
Last synced: 10 months ago
JSON representation
defines static object attributes using `Object.defineProperties`
- Host: GitHub
- URL: https://github.com/fibo/static-props
- Owner: fibo
- License: mit
- Created: 2016-03-06T13:59:38.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-05-10T20:28:56.000Z (almost 7 years ago)
- Last Synced: 2025-03-28T17:11:12.203Z (about 1 year ago)
- Language: JavaScript
- Homepage: http://g14n.info/static-props
- Size: 41 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-micro-npm-packages-zh - static-props - 使用`Object.defineProperties`定义静态对象属性 (模块 / 对象)
- awesome-micro-npm-packages - static-props - Defines static object attributes using `Object.defineProperties` (Modules / Object)
- fucking-awesome-micro-npm-packages - static-props - Defines static object attributes using `Object.defineProperties` (Modules / Object)
- awesome-micro-npm-packages - static-props - Defines static object attributes using `Object.defineProperties` (Modules / Object)
README
# static-props
> defines static object attributes using `Object.defineProperties`
[Installation](#installation) |
[Usage](#usage) |
[Annotated source](#annotated-source) |
[License](#license)
[](http://badge.fury.io/js/static-props)
[](https://travis-ci.org/fibo/static-props?branch=master)
[](https://github.com/fibo/strict-mode)
[](https://standardjs.com)
[](http://g14n.info/kiss-literate-programming)
## Installation
With [npm](https://npmjs.org/) do
```bash
npm install static-props --save
```
## Usage
Let's create a classic Point2d class and add constant attributes.
```js
'use strict'
const staticProps = require('static-props')
// Or use ES6: import staticProps from '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.
const enumerable = true
staticProps(this)({x, y}, enumerable)
}
}
// Add a static class attribute.
staticProps(Point2d)({ dim: 2 })
const norm = (x, y) => x * x + y * y
// A particular case are static methods, since they are functions
// they must be wrapped otherwise are considered as getters.
staticProps(Point2d)({ norm: () => norm })
```
After instantiating the class, we can check that its props cannot be changed.
```js
const p = new Point2d(1, 2)
// Trying to modify a static prop will throw, as expected.
p.label = 'B'
// TypeError: Cannot assign to read only property 'label' of #
```
Props *label* and *color* are values, while *norm* is a getter.
```js
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*.
```js
console.log(p) // Point2d { x: 1, y: 2 }
```
You can access static class attributes and methods.
```js
console.log(Point2d.dim) // 2
console.log(Point2d.norm(1, 2)) // 5
```
## Annotated source
API is `staticProps(obj)(props[, enumerable])`.
Add every *prop* to *obj* as not writable nor configurable, i.e. **static**.
If prop is a function use it as a *getter*, otherwise use it as a *value*.
Finally, apply the [Object.defineProperties](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties) function.
```javascript
/**
* @param {Object} obj
* @returns {Function}
*/
function staticProps (obj) {
/**
* @param {Object} props
* @param {Boolean} [enumerable]
*/
return function (props, enumerable) {
var staticProps = {}
for (var propName in props) {
var staticProp = {
configurable: false,
enumerable: enumerable
}
var prop = props[propName]
if (typeof prop === 'function') {
staticProp.get = prop
} else {
staticProp.value = prop
staticProp.writable = false
}
staticProps[propName] = staticProp
}
Object.defineProperties(obj, staticProps)
}
}
```
Export function, supporting both CommonJS and ES6.
```javascript
module.exports = exports.default = staticProps
```
## License
[MIT](http://g14n.info/mit-license)