Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32116

Re: Why is eval( "05" ) a direct eval call in ECMAScript?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Why is eval( "05" ) a direct eval call in ECMAScript?
Date 2017-01-06 00:20 +0100
Organization PointedEars Software (PES)
Message-ID <11687474.uLZWGnKmhe@PointedEars.de> (permalink)
References <direct-eval-call-20170105204036@ram.dialup.fu-berlin.de>

Show all headers | View raw


Stefan Ram wrote:

>   I am going to try to deduce why »eval( "05" )« is a
>   direct call of eval in ECMAScript. […]

Because you are not using the return value of eval().

There are syntactical *and* a functional differences between each of the 
following statements:

(function () {
  var v = 42;

  /* Modifies the local v;
     creates global “v” property if “v” cannot be resolved */
  eval("v = 23;");

  console.log(v);
}());

(function () {
  "use strict";

  var v = 42;

  /* Modifies the local v;
     throws ReferenceError if “v” cannot be resolved */
  eval("v = 23;");

  console.log(v);
}());


(function () {
  var v = 42;

  /* Modifies the local v; can declare the local v if not declared */
  eval("var v = 23;");

  console.log(v);
}());

(function () {
  "use strict";

  var v = 42;

  /* Declares and modifies the eval v, not the local v */
  eval("var v = 23;");

  console.log(v);
}());


(function () {
  var v = 42;

  /* Modifies the local v and x;
     create global “v” property if “v” cannot be resolved */
  var x = eval("v = 23;");

  console.log(v);
}());

(function () {
  "use strict";

  var v = 42;

  /* Modifies the local v and x;
     throws ReferenceError if “v” cannot be resolved
   */
  var x = eval("v = 23;");

  console.log(v);
}());


(function () {
  var v = 42;

  /* Redeclares and modifies the local v */
  var x = eval("var v = 23;");

  console.log(v);
}());

(function () {
  "use strict";

  var v = 42; 

  /* Declares and modifies the eval v, not the local v */
  var x = eval("var v = 23;");

  console.log(v);
}());


Also, in your case the code does not work in strict mode because "05" is an 
octal integer literal (produced by /OctalIntegerLiteral/):

| > eval("05")
| 5
| 
| > (function () { "use strict"; eval("05"); }());
| Uncaught SyntaxError: Octal literals are not allowed in strict mode.(…)
| 
| > (function () { var x = eval("05"); }());
| undefined
| 
| (function () { "use strict"; var x = eval("05"); }());
| Uncaught SyntaxError: Octal literals are not allowed in strict mode.(…)
| 
| > navigator.userAgent
| "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) 
| Chrome/53.0.2785.143 Safari/537.36"

(That is V8 JavaScript 5.3.332.47, see <chrome://version/>)

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread


Thread

Re: Why is eval( "05" ) a direct eval call in ECMAScript? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-06 00:20 +0100

csiph-web