https://github.com/serapath/deepfreeze
recursively Object.freeze() on objects and functions with properties
https://github.com/serapath/deepfreeze
Last synced: about 1 year ago
JSON representation
recursively Object.freeze() on objects and functions with properties
- Host: GitHub
- URL: https://github.com/serapath/deepfreeze
- Owner: serapath
- License: mit
- Created: 2016-09-05T20:13:00.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-09-06T04:36:00.000Z (almost 10 years ago)
- Last Synced: 2024-11-02T05:38:46.263Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/deepfreeze
- Size: 2.93 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# deepfreeze
recursively Object.freeze() on objects and functions with properties
# usage
```js
var deepfreeze = require('deepfreeze')
/*******************************************************/
Person.prototype.say=function(){console.log('hi')}
Person.prototype.bye=function(){console.log('bye')}
function Person (name) {
return { __proto__: Person.prototype, name: name }
}
Barterer.prototype.__proto__ = Person.prototype
Barterer.prototype.clear=function(){this.inventory=[]}
function Barterer (name, inventory) {
var o = Person(name)
o.__proto__ = Barterer.prototype
o.inventory = inventory
return o
}
/*******************************************************/
var tom = new Barterer('tom', [{sugar:1},{salt:2}])
deepfreeze(tom)
tom.name = 'bill'
console.log(tom.name) // => 'tom'
tom.inventory = ['m3h']
console.log(tom.inventory) // [{sugar:1},{salt:2}]
tom.inventory.push({x:3}) // throws
deepfreeze(tom.__proto__)
tom.__proto__.clear=function (){ return 7}
tom.clear() // => 'clear'
tom.__proto__.say=function (){ return 6 }
tom.say() // => 'say'
tom.__proto__.bye=function (){ return 5 }
tom.bye() // => 'bye'
```