Skip to content
Browse files

added function example

  • Loading branch information...
1 parent 0025230 commit 9c77a24897e9e312f2e346e0838356502d9b92e7 @hunterloftis committed
Showing with 39 additions and 4 deletions.
  1. +23 −2 README.md
  2. +13 −0 examples/function.js
  3. +3 −2 examples/simple.js
View
25 README.md
@@ -35,15 +35,16 @@ var Cryo = require('cryo');
var obj = {
name: 'Hunter',
+ created: new Date(),
hello: function() {
- console.log(this.name + ' says hello!');
+ console.log(this.name + ' said hello in ' + this.created.getFullYear() + '!');
}
};
var frozen = Cryo.stringify(obj);
var hydrated = Cryo.parse(frozen);
-hydrated.hello(); // Hunter says hello!
+hydrated.hello(); // Hunter says hello in 2013!
```
## More powerful JSON
@@ -147,7 +148,27 @@ console.log(withCryo.second.third.fourth.name === 'Hunter'); // true
### Functions
+Cryo will stringify functions, which JSON ignores.
+**Note:** Usually, if you've come up with a solution that needs to serialize functions, a better solution exists that doesn't.
+However, sometimes this can be enormously useful.
+Cryo will make faithful hydrated functions and objects with properties that are functions.
+
+```js
+var Cryo = require('../lib/cryo');
+
+function fn() {}
+
+try {
+ var withJSON = JSON.parse(JSON.stringify(fn));
+ console.log(typeof withJSON === 'function');
+} catch(e) {
+ console.log('error'); // error
+}
+
+var withCryo = Cryo.parse(Cryo.stringify(fn));
+console.log(typeof withCryo === 'function'); // true
+```
## Tests
View
13 examples/function.js
@@ -0,0 +1,13 @@
+var Cryo = require('../lib/cryo');
+
+function fn() {}
+
+try {
+ var withJSON = JSON.parse(JSON.stringify(fn));
+ console.log(typeof withJSON === 'function');
+} catch(e) {
+ console.log('error'); // error
+}
+
+var withCryo = Cryo.parse(Cryo.stringify(fn));
+console.log(typeof withCryo === 'function'); // true
View
5 examples/simple.js
@@ -2,12 +2,13 @@ var Cryo = require('../lib/cryo');
var obj = {
name: 'Hunter',
+ created: new Date(),
hello: function() {
- console.log(this.name + ' says hello!');
+ console.log(this.name + ' said hello in ' + this.created.getFullYear() + '!');
}
};
var frozen = Cryo.stringify(obj);
var hydrated = Cryo.parse(frozen);
-hydrated.hello(); // Hunter says hello!
+hydrated.hello(); // Hunter says hello in 2013!

0 comments on commit 9c77a24

Please sign in to comment.
Something went wrong with that request. Please try again.