473,699 Members | 3,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.anotherF unc() 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 6579
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.c om/group/comp.lang.javas cript/browse_frm/thread/1d67f0e7b79fa9b 7/67ff9bdcd3b8bb3 8#67ff9bdcd3b8b b38>

<http://groups.google.c om/group/comp.lang.javas cript/browse_frm/thread/bcadfcbde01a0b7 7/565e6accfcd8cd1 d>

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.javas cript 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
1468
by: africamp | last post by:
Lets say I have a program: #include <iostream> #include <conio.h> using namespace std; int* ptest(); int main() {
2
2033
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 the finally block is existing there cuz of the logic inside try block, if try is not there finally wont be there. If try and finally are so closely related than finally should share the scope of try. Please explain the rational behind this scope...
10
1879
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 namespace) or built-in (in the predefined __builtin__ names module Python provides." I have trouble reading this sentence. First, I don't understand if the word 'enclosing' is a verb or an adjective. The whole flow of the sentence seems...
10
2120
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 member variable. Therefore, it would appear that the scope of a static class member is limited to a specific instance of an application that happens to be running on the machine.
5
2235
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
8633
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8947
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6552
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4392
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2366
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.