Reputation: 2783
NOTE: I do realize that there's currently low browser support for the
uneval
function, however I am in interest to know what it does, and how it works.
I know that eval()
executes javascript code as a string, but uneval()
seems strange to me. It appears that it unevaluates code, judging by one of its uses, deep object cloning. For example:
let myObject = { nonsense: null };
var newObject = eval( uneval(myObject) );
// The above example deeply clones myObject in Mozilla Firefox
But how does the magic work just like that? What does uneval
do, that allows such uses? More importantly, what could the other uses be? As it seamlessly unevaluated evaluated content; the real test is, can it unevaluate anything else besides content passed through the eval()
function (Or in this particular case, vice versa)?
Upvotes: 3
Views: 1755
Reputation: 46
The uneval
function returns the source of a given object; whereas the eval
function does the opposite, by evaluating that source code. Think of it this way, you get the source of an object, then you evaluate the source to get the same object, just in a different memory area. Here's a link to a good resource on the mom documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/uneval, as mentioned in the comments.
Upvotes: 3