Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2806982.D5yMYtsCpS@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Mon, 10 Feb 2014 16:37:34 +0100 User-Agent: KNode/4.11.3 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. MIME-Version: 1.0 Lines: 66 NNTP-Posting-Date: 10 Feb 2014 16:37:52 CET NNTP-Posting-Host: 5533ad28.newsspool2.arcor-online.net X-Trace: DXC=?T37YU`1:d6^8FBo0_81f>A9EHlD;3Yc24Fo<]lROoR18kF I observe that under Firefox > > console.log( eval( "{1;}" )); > > prints > > 1 This should not only be so in Firefox, i.e. with Mozilla JavaScript, but with all ECMAScript implementations whose host environment or user code provide an adequate method. > . Now I'd like to simplify the block evaluation. > I'd like to get rid of the »eval«. However, > > console.log( {1;} ); > > gives an error > > SyntaxError: missing : after property id Works as designed. > . What is the simplemost console.log statement > that shows the value of the block? I think you have found it already. You appear to be unaware of the object initialiser [1], which is syntactically similar to a /Block/ statement [2]. (BTW, support for block *scoping* can AFAIK be found only in Mozilla JavaScript 1.7 with the “let” keyword; it has not been standardized yet.) With console.log(…), particularly within the /ArgumentList/ of the /CallExpression/ the parser is in /Expression/ context [3], and “{…}” is not going to be evaluated as a /Block/ statement, but as an /ObjectLiteral/. This is different with eval(), because the *string* argument is evaluated as a /Program/; as the parser is initially not in /Expression/, but in /Statement/ context, “{…}” will be evaluated as a /Block/ statement if possible [4] (otherwise it is a syntax error). For that reason you need to enforce /Expression/ context evaluation if you want to evaluate generated object initalisers; the most efficient way is to put the generated expression in parentheses [5]: var obj = eval("(" + foo + ")"); Whereas “foo” could have a value of, e.g., '{"bar": "baz"}'. However, when evaluating JSON, the safest way is to use JSON.parse(), provided it is available (either built-in or through a library) [6][7]. ___________ [1] [2] [3] [4] [5] [6] [7] -- PointedEars Twitter: @PointedEars2 Please do not Cc: me. / Bitte keine Kopien per E-Mail.