JSFiddle - React, Tailwind, and code Playground
by Ahmad Baktash Hayeri
JavaScript
function CaveMan(name) {
this.name = name;
};
CaveMan.prototype.getName = function () {
return this.name + ", the cave man";
}
CaveMan.freeze = function (cm) {
return {
_class : 'CaveMan',
n : cm.name
};
};
// Static method to reconstitute a CaveMan from the basic structure
CaveMan.thaw = function (o) {
return new CaveMan(o.n);
};
var example = {
cryo : function (k,o) {
console.log(k);
return (o instanceof CaveMan) ? CaveMan.freeze(o) : o;
},
revive : function (k,v) {
// Check for cavemen by the _class key
if (v instanceof Object && v._class == 'CaveMan') {
return CaveMan.thaw(v);
}
// default to returning the value unaltered
return v;
}
};
/*
example.data = {
count : 1,
type : 'Hominid',
specimen : [
new CaveMan('Baktash'),
new CaveMan('Rohina')
]
}; */
example.data = {
data: {
validator: new CaveMan('Baktash'),
id: 20093,
arr : [new String("Hello World")]
}
};
example.jsonString = JSON.stringify(example.data, function(key, value){
console.log(key);
return value;
});
//console.log(example);
var x = JSON.parse(example.jsonString, example.revive);
//console.log(x)