Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <2261899.gBobAo4G4S@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 09 Nov 2012 17:46:59 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 34 NNTP-Posting-Date: 09 Nov 2012 17:46:59 CET NNTP-Posting-Host: f55ee6ae.newsspool2.arcor-online.net X-Trace: DXC=4U7lK`ehLF0A91V>hW[[7HSFnmUo X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17100 Cezary Tomczyk wrote: > According to "Better not use eval() because ... ": > > How to use eval in a correct way? > > For example when I'll get the JavaScript (or any other EcmaScript > implementation) code through XMLHttpRequest then how to safely eval the > code? (It's _ECMAScript_, published by _Ecma_ International, for historical reasons.) The only way to safely use eval() is a) not to use it or b) to be sure that you are using a conforming implementation of ECMAScript Ed. 5 or later (where the scope chain of eval code is empty; which you really cannot be sure about in a browser). But if you really must use eval(), and want to/need to parse JSON(-like data) with it (instead of with JSON.parse()), be sure that the literal is evaluated in an expression context: var obj = eval("(" + JSON_object_string + ")"); The `(' and `)' would be necessary because the `{' and `}' of an object literal also delimit a Block statement, whose production takes precedence in a statement context. PointedEars -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, (2004)