https://github.com/eimfach/frozen-core
frozen-core.js - immutable objects
https://github.com/eimfach/frozen-core
experimental immutable javascript object-creation opinionated
Last synced: 4 months ago
JSON representation
frozen-core.js - immutable objects
- Host: GitHub
- URL: https://github.com/eimfach/frozen-core
- Owner: eimfach
- License: mit
- Created: 2014-12-09T23:20:54.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-09-10T14:20:12.000Z (almost 8 years ago)
- Last Synced: 2025-06-11T14:12:51.697Z (about 1 year ago)
- Topics: experimental, immutable, javascript, object-creation, opinionated
- Language: JavaScript
- Homepage:
- Size: 41 KB
- Stars: 3
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# frozen-core

[](https://coveralls.io/r/eimfach/capsule-js?branch=master)

[](https://codeclimate.com/github/eimfach/capsule-js)
Experimental lib to create immutable objects and structures in a very opinionated way.
- Very small (4KB, raw)
- Object properties are immutable
- Optional state properties
- Simple Typesafe inheritance
- Hierarchic method bubbling
- Enforces a specifc way to implement your Objects and Methods
```javascript
var frozenCore = require('frozen-core');
var myObject = frozenCore.extend({
state: {
//mutable property values (the object mask)`
},
core: {
// immutable property values here
//only functions allowed (other types are ommited)
// 'shares' the state object scope by obtaining a immutable copy (the snapshot) (lexical this refers to that snapshot)
/* Every method here only can access that copy but no other method within
the object except the inherited public api methods (extend, bubble) */
}
})
// the properties of the resulting object are always immutable, not configurable as the object itself too, one can't add new properties, remove or configure them
```
## API
```javascript
myObject.extend(/*options object*/);
myObject.bubble(/*method to call (which will be executed on every parent)*/)
myObject.getSnapshot() //returns a reference to the immutable state copy
myObject.parent // reference to the latest origin
```
## Simple implementation example
```javascript
var cube = frozenCore.extend({
core: {
mutate: function(){
//'this' is safe to use here since it will be immutable - it will refer to a copy of the state object
/* this will have no effect except throwing an error in strict mode !*/this.width = 1;
return this.extend({
state: {
width: this.width+20,
height: this.height+20,
depth: this.depth+20
}
});
}
},
state: {
width: 0,
height: 0,
depth: 0
}
});
var mutatedCube = cube.mutate();
```