Last active
February 29, 2016 12:15
-
-
Save oprearocks/ad8b0c32fb5baac50017 to your computer and use it in GitHub Desktop.
[ARTICLE] /serializing-object-methods-with-es6-template-literals-and-eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let reviver = (key, value) => { | |
if (typeof key === 'string' && key.indexOf('function ') === 0) { | |
let functionTemplate = `(${value})`; | |
return eval(functionTemplate); | |
} | |
return value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let reviver = (key, value) => { | |
if (typeof value === 'string' && value.indexOf('function ') === 0) { | |
let functionTemplate = `(${value}).call(this)`; | |
return new Function(functionTemplate); | |
} | |
return value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"function () { console.log('Susan says hi!'); }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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