What is the function of uneval?

5

I was seeing some things in Javascript (which seems novelty to me) and I came across an example that showed a function called uneval .

Some questions below about it:

  • This function is new in Javascript (are we in 2018)?

  • Does it do the "opposite" of eval ?

  • I also read that it should not be used in production. Does using it pose any risk to my application?

  • It works in which browsers?

asked by anonymous 13.07.2018 / 00:24

1 answer

4

Response : Not at all practical.

Given the content on the internet, I believe that the uneval function was created by the Mozilla team under some internal demand and ended up leaving the role there - maybe Firefox even use it for something.

Compatibility

It is, in fact, unique to Firefox and is not provided for in any current specification, so no kind of prediction or even discussing implement it in other browsers, so in practice it will really have no use at all - unless you have an application that runs exclusively on Firefox, such as a browser extension, for example.

Nomobileisnodifferent.

Function

Butwhatdoesitdo?Well,inasimplifiedway,itdoestheinverseof eval . While eval constructs an object from its representation as string , the uneval function constructs a string representing an object. For example, when doing uneval(function foo(){}); the return will be the string "(function foo(){})" and being a string , you can do anything that can be done with a string : display it in console , send via HTTP, etc. In PHP there is a var_export function that has a similar function.

For example, you could show the source code of a function in console.log , along with an error message, to facilitate debug or even to feed some other type of log - be aware that there are lots of better ways to do this, so do not do this:

  

Note: The example below will only work in Firefox for the above reasons.

function sum(a, b) {
  if (isNaN(a) || isNaN(b)) {
    throw "Desculpe, sei apenas somar números";
  }
  
  return a + b;
}

try {
  const result = sum(2, 'a');
} catch (error) {
  console.log('Falha na função sum(a, b):', error);
  console.log( uneval(sum) );
}

So the output in console would be:

Falha na função sum(a, b): Desculpe, sei apenas somar números
function sum(a, b) {
  if (isNaN(a) || isNaN(b)) {
    throw "Desculpe, sei apenas somar números";
  }

  return a + b;
}

But let's face it, nor is it such a useful application.

And the method Object.prototype.toString ?

Both seem to do the same thing: return the string representation of an object, however, the toString method does not care about generating a string that is a valid JavaScript code, while uneval yes. If you take the representation of both of a JS object it is easy to see the difference:

const obj = new Object()

console.log(obj.toString())  // [object Object]
console.log(uneval(obj))  // ({})

To demonstrate the validity of JavaScript code, simply apply the eval function:

console.log( eval(obj.toString()) )  // Erro
console.log( eval(uneval(obj)) ) // Object {  }

Although the combination eval + uneval recreates the object, it does not mean that it will be the same object.

obj == eval(uneval(obj))  // false
    
13.07.2018 / 19:04