Skip to content

Instantly share code, notes, and snippets.

@oprearocks
Last active February 29, 2016 12:15
Show Gist options
  • Save oprearocks/ad8b0c32fb5baac50017 to your computer and use it in GitHub Desktop.
Save oprearocks/ad8b0c32fb5baac50017 to your computer and use it in GitHub Desktop.
[ARTICLE] /serializing-object-methods-with-es6-template-literals-and-eval
//ES6 template strings
let someComputedValue = 5 + 3;
let theTemplate = `This is the result of the computation: ${someComputedValue}`;
console.log(theTemplate);
// "This is the result of the computation: 8"
// ES5 version
var someComputedValue = 5 + 3;
var theTemplate = 'This is the result of the computation: ' + someComputedValue;
console.log(theTemplate);
// "This is the result of the computation: 8"
'use strict';
// serialize.js
let person = {
name: 'Susan',
age: 24,
sayHi: function() {
console.log('Susan says hi!');
}
};
let replacer = (key, value) => {
// if we get a function, give us the code for that function
if (typeof value === 'function') {
return value.toString();
}
return value;
}
// get a stringified version of our object, and indent the keys at 2 spaces
const serialized = JSON.stringify(person, replacer, 2);
console.log(serialized);
// {"name":"Susan","age":24,"sayHi":"function () {\n\tconsole.log('Susan says hi!');\n }"}
// de_serialize.js
let reviver = (key, value) => {
if (typeof value === 'string' && value.indexOf('function ') === 0) {
let functionTemplate = `(${value})`;
return eval(functionTemplate);
}
return value;
}
const parsedObject = JSON.parse(serialized, reviver);
parsedObject.sayHi(); // Susan says hi!
let reviver = (key, value) => {
if (typeof key === 'string' && key.indexOf('function ') === 0) {
let functionTemplate = `(${value})`;
return eval(functionTemplate);
}
return value;
}
let reviver = (key, value) => {
if (typeof value === 'string' && value.indexOf('function ') === 0) {
let functionTemplate = `(${value}).call(this)`;
return new Function(functionTemplate);
}
return value;
}
JSON.parse(text[, reviver])
// => text(required) - the string we wish to de-serialize and convert back
// to a standard JavaScript object(JSON)
// => reviver(optional) - function used to pre-process keys and values in order
// to render a specific object structure
'use strict';
let person = {
name: 'Susan',
age: 24,
sayHi: function() {
console.log('Susan says hi!');
}
};
const serialized = JSON.stringify(person);
console.log(serialized); // {"name":"Susan","age":24}
typeof serialized === 'string' // true
"function () { console.log('Susan says hi!'); }"
JSON.stringify(value[, replacer[, space]])
// => value(required) - the object we want to serialize
// => replacer(optional) - a function that will be called for each of the
// object's properties, or an array of strings and numbers that serve
// as a whitelist for the property selection process
// => space(optional) - the number of spaces each key will receive as indent
'use strict';
let person = {
name: 'Susan',
age: 24,
sayHi: function() {
console.log('Susan says hi!');
}
};
let replacer = (key, value) => {
// if we get a function, give us the code for that function
if (typeof value === 'function') {
return value.toString();
}
return value;
}
// get a stringified version of our object, and indent the keys at 2 spaces
const serialized = JSON.stringify(person, replacer, 2);
console.log(serialized);
// {"name":"Susan","age":24,"sayHi":"function () {\n\tconsole.log('Susan says hi!');\n }"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment