Java Virtal Machine.net

[ News ] rss

February 01, 2011

Mark Wielaard: New GPG key. Finally created a new GPG key using gnupg. The old one was a DSA/1024 bits one and 8 years old. The new one is a RSA/2048 bits one. I will use the new one in the future to sign any release tarballs I might create. pub 2048R/57816A6A 2011-01-29 Key f...

More »

February 01, 2011

Andrew Hughes: [SECURITY] IcedTea6 1.7.8, 1.8.5, 1.9.5 Released!. We are pleased to announce a new set of security releases, IcedTea6 1.7.8, IcedTea6 1.8.5 and IcedTea6 1.9.5. This update contains the following security updates: The IcedTea project provides a harness to build the source code from OpenJDK6 u...

More »

March/2024
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
242526272829 30
31      

[ Archives News
for 'Java Technology' ]

home > news > java technology > eval javascript in a global context

Eval JavaScript in a global context

Even though it's considered bad practice, it's often handy to eval code in JavaScript.  And in my case, it was simply necessary, since the JSF specification requires eval of scripts. And it's also necessary to execute those evaluated scripts in the global scope. It's not as easy as it first looks. For our first naive implementation, we'd simply used eval(src) in our first pass at the implementation. This is utterly wrong, and to understand why, you'll need to understand scopes. JavaScript has what you can think of as two different scopes - function scope, where you're executing something in the context of a function, and global scope, where you're executing something in a global context - for instance, if I say var j = 1; within a function's scope, then the variable j is set to 1 within that function. If I say the same expression, var j = 1 within the global scope, then j is set to 1 everywhere in the program - in every function, provided that that function doesn't define a j variable in its local scope. In browsers, the global context is window - this is the default object that everything gets hung off of if you don't specify any other object. So, when we said eval(src), we were executing the src scripts within the local scope of the function where eval was called - that meant that I would be getting different results when variables were declared and set than wou...


Date: September, 08 2009
Url: http://www.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context


Others News

©2002-2019Java-Virtual-Machine.net free website generator | mobiriseextensions.com | best website design software | free bootstrap template | css menu