{"id":15355857,"url":"https://github.com/fibo/static-props","last_synced_at":"2025-06-15T21:19:06.099Z","repository":{"id":57370093,"uuid":"53257582","full_name":"fibo/static-props","owner":"fibo","description":"defines static object attributes using `Object.defineProperties`","archived":false,"fork":false,"pushed_at":"2019-05-10T20:28:56.000Z","size":42,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T17:11:12.203Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://g14n.info/static-props","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fibo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-06T13:59:38.000Z","updated_at":"2024-05-31T06:53:35.000Z","dependencies_parsed_at":"2022-09-16T22:52:10.425Z","dependency_job_id":null,"html_url":"https://github.com/fibo/static-props","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fstatic-props","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fstatic-props/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fstatic-props/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fibo%2Fstatic-props/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fibo","download_url":"https://codeload.github.com/fibo/static-props/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248986316,"owners_count":21194025,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T12:26:02.432Z","updated_at":"2025-04-15T06:38:16.073Z","avatar_url":"https://github.com/fibo.png","language":"JavaScript","readme":"# static-props\n\n\u003e defines static object attributes using `Object.defineProperties`\n\n[Installation](#installation) |\n[Usage](#usage) |\n[Annotated source](#annotated-source) |\n[License](#license)\n\n[![NPM version](https://badge.fury.io/js/static-props.svg)](http://badge.fury.io/js/static-props)\n[![Build Status](https://travis-ci.org/fibo/static-props.svg?branch=master)](https://travis-ci.org/fibo/static-props?branch=master)\n[![No deps](https://img.shields.io/badge/dependencies-none-green.svg)](https://github.com/fibo/strict-mode)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n[![KLP](https://img.shields.io/badge/kiss-literate-orange.svg)](http://g14n.info/kiss-literate-programming)\n\n## Installation\n\nWith [npm](https://npmjs.org/) do\n\n```bash\nnpm install static-props --save\n```\n\n## Usage\n\nLet's create a classic Point2d class and add constant attributes.\n\n```js\n'use strict'\n\nconst staticProps = require('static-props')\n// Or use ES6: import staticProps from 'static-props'\n\nclass Point2d {\n  constructor (x, y, label) {\n    // Suppose you have few static attributes in your class.\n    const color = 'red'\n\n    // Create a getter.\n    const norm = () =\u003e x * x + y * y\n\n    // Add constant attributes quickly.\n    staticProps(this)({label, color, norm})\n\n    // Add enumerable attributes.\n    const enumerable = true\n    staticProps(this)({x, y}, enumerable)\n  }\n}\n\n// Add a static class attribute.\nstaticProps(Point2d)({ dim: 2 })\n\nconst norm = (x, y) =\u003e x * x + y * y\n// A particular case are static methods, since they are functions\n// they must be wrapped otherwise are considered as getters.\nstaticProps(Point2d)({ norm: () =\u003e norm })\n```\n\nAfter instantiating the class, we can check that its props cannot be changed.\n\n```js\nconst p = new Point2d(1, 2)\n\n// Trying to modify a static prop will throw, as expected.\np.label = 'B'\n// TypeError: Cannot assign to read only property 'label' of #\u003cPoint2d\u003e\n```\n\nProps *label* and *color* are values, while *norm* is a getter.\n\n```js\nconsole.log(p.label) // 'A'\nconsole.log(p.color) // 'red'\nconsole.log(p.norm) // 5 = 1 * 1 + 2 * 2\n```\n\nAttributes `x`, `y` were configured to be *enumerable*.\n\n```js\nconsole.log(p) // Point2d { x: 1, y: 2 }\n```\n\nYou can access static class attributes and methods.\n\n```js\nconsole.log(Point2d.dim) // 2\nconsole.log(Point2d.norm(1, 2)) // 5\n```\n\n## Annotated source\n\nAPI is `staticProps(obj)(props[, enumerable])`.\n\nAdd every *prop* to *obj* as not writable nor configurable, i.e. **static**.\nIf prop is a function use it as a *getter*, otherwise use it as a *value*.\nFinally, apply the [Object.defineProperties](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties) function.\n\n```javascript\n/**\n * @param {Object} obj\n * @returns {Function}\n */\nfunction staticProps (obj) {\n  /**\n   * @param {Object} props\n   * @param {Boolean} [enumerable]\n   */\n  return function (props, enumerable) {\n    var staticProps = {}\n\n    for (var propName in props) {\n      var staticProp = {\n        configurable: false,\n        enumerable: enumerable\n      }\n\n      var prop = props[propName]\n\n      if (typeof prop === 'function') {\n        staticProp.get = prop\n      } else {\n        staticProp.value = prop\n\n        staticProp.writable = false\n      }\n\n      staticProps[propName] = staticProp\n    }\n\n    Object.defineProperties(obj, staticProps)\n  }\n}\n```\n\nExport function, supporting both CommonJS and ES6.\n\n```javascript\nmodule.exports = exports.default = staticProps\n```\n\n## License\n\n[MIT](http://g14n.info/mit-license)\n","funding_links":[],"categories":["模块","Modules"],"sub_categories":["对象","Object"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibo%2Fstatic-props","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffibo%2Fstatic-props","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffibo%2Fstatic-props/lists"}