473,698 Members | 2,158 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 6578
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
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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
9031
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
8901
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,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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
4371
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...
3
2007
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.