473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Question about eval scope

Could someone explain what the scope chain looks like to code running
inside eval? (Please no "eval is the devil" essays :) ) Let's say I
have a string of JavaScript that looks like this:

var myVar1 = 1;
var myVar2 = 3;

window.someFunc = function() { myVar1++; alertFunc(); };

function anotherFunc() { myVar2++; alertFunc(); }

function alertFunc() { alert(myVar1 + ' ' + myVar2); }

If I run that through eval (from within another function), I can then
do this:

window.someFunc();

and it'll show 1, then 2, then 3, etc. as I call it multiple times. So
I know that myVar1 is hanging around in memory somewhere along with
alertFunc. I know it's not attached to window, since
window.anotherFunc() gives me a standard "undefined" error.

I've also tried doing the eval like this:

function myFunction() { window.someObj = eval(theString); }

I was told this would attach the stuff defined in theString to someObj,
but window.someObj.someFunc is still undefined.

So what I'm wondering is, where are the variables and functions I
didn't explicitly assign to window defined? I've looked at the very
excellent FAQ article regarding scope chains and closures, but it
didn't cover what happens inside eval. Anyone know? Thanks in advance
for any insight.

Jan 16 '06 #1
4 6559
VK

jdc_1040 wrote:
var myVar1 = 1;

window.someFunc = function() { myVar1++; alertFunc(); };

function anotherFunc() { myVar2++; alertFunc(); }

function alertFunc() { alert(myVar1 + ' ' + myVar2); }

If I run that through eval (from within another function), I can then
do this:

window.someFunc();

and it'll show 1, then 2, then 3, etc. as I call it multiple times. So
I know that myVar1 is hanging around in memory somewhere along with
alertFunc.


What do you mean "hanging around"? :-)
In the posted code iIt is a properly declared global variable so it is
not ""hanging" but allocated in the script engine program stack.

Irrelevant to "eval" and besides FAQ's you should learn two higher
level scopes implemented by modern IE: DOM Document scope and Frame
scope.

They are not documented (yet?) but are very useful to know:
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/1d67f0e7b79fa9b7/67ff9bdcd3b8bb38#67ff9bdcd3b8bb38>

<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/bcadfcbde01a0b77/565e6accfcd8cd1d>

Jan 16 '06 #2
jdc_1040 said the following on 1/16/2006 4:46 PM:
Could someone explain what the scope chain looks like to code running
inside eval? (Please no "eval is the devil" essays :) ) Let's say I
have a string of JavaScript that looks like this:


eval is executed in the current scope. So, its scope has the same scope
as if you weren't using eval. Nothing special about eval in that regards
as it doesn't change the scope of what its executing.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 16 '06 #3
Ok, "hanging around" was a bad choice of words. :) I understand that
it is properly instantiated. I'm just wondering what object has
properties allowing me to reach them - if any. I'm not sure that the
DOM Document scope and Frame scope would have any bearing if they are
specific to IE as I'm seeing the same results in Firefox, but I'll
check out the links you gave and see if I get any insight from them.
Thanks for the reply.

Jan 16 '06 #4
Thanks Randy. Now that I think about it, that makes total since given
the behavior I was seeing, I was probably just staring at it too long
to see what was going on. :)

Jan 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: africamp | last post by:
Lets say I have a program: #include <iostream> #include <conio.h> using namespace std; int* ptest(); int main() {
2
by: Abubakar | last post by:
Hi all, The variables declared inside try block cant be accessed inside its corresponding finally block. When I used to explain finally concept to anyone I would usually tell them (briefly) that...
10
by: John Salerno | last post by:
Here's a sentence from Learning Python: "Names not assigned a value in the function definition are assumed to be enclosing scope locals (in an enclosing def), globals (in the enclosing module's...
10
by: newjazzharmony | last post by:
All, When I create a class with a static member variable and reference that class in a console applicaton it appears as though each instance of the application has its own separate copy of the...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.