473,385 Members | 1,912 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,385 software developers and data experts.

variable instantiation & eval

hi,
What should be the output of the following code-snippet?
===
var x = "global";
function f() {
var x = 0;
eval("function x() { return false; }");
delete x;
alert(x);
}
f();
===
- Rhino (1.6 release 5), Opera (9.23) and Konqueror (3.5.7) print
"function x() { return false; }"
- Firefox (2.0.0.6) prints "0".
- and I think it should be "global"...

Relevant sections of the spec are: 10.1.3 where it states: "... If the
variable object already has a property with this name, replace its value
and attributes",
and 10.2.2 with "Variable instantiation is performed using the calling
context's variable object and using empty property attributes".

mfg,
// florian loitsch

Sep 11 '07 #1
2 3647
Florian Loitsch wrote:
What should be the output of the following code-snippet?
===
var x = "global";
function f() {
var x = 0;
eval("function x() { return false; }");
delete x;
You might want to check the return value of that delete x, knowing that
is essential I think to understand what the alert later outputs.
It seems the implementations don't delete x (i.e. delete x yields
false), that is why
alert(x);
}
f();
===
- Rhino (1.6 release 5), Opera (9.23) and Konqueror (3.5.7) print
"function x() { return false; }"
those give the function as the result.
- Firefox (2.0.0.6) prints "0".
That seems odd to me and looks like a bug in the handling of the eval
call with the function declaration.
- and I think it should be "global"...
That seems to be the right result if the delete x yielded true.

The specification of the var statement says:
"If the variable statement occurs inside a FunctionDeclaration, the
variables are defined with function-local scope in
that function, as described in section 10.1.3. Otherwise, they are
defined with global scope (that is, they are created
as members of the global object, as described in section 10.1.3) using
property attributes { DontDelete }."

One could read that as saying that only a global variable declaration
should create a property with "DontDelete", in that case your
expectation of the result of your test case showing "global" seems right
but I a bit doubtful of my analysis with all implementations seemingly
agreeing on setting "DontDelete" on the local var statement too.

Let's hear some other opinions.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 11 '07 #2
Martin Honnen wrote:
Florian Loitsch wrote:
>What should be the output of the following code-snippet?
===
var x = "global";
function f() {
var x = 0;
eval("function x() { return false; }");
delete x;

You might want to check the return value of that delete x,
knowing that is essential I think to understand what the
alert later outputs. It seems the implementations don't
delete x (i.e. delete x yields false), that is why
> alert(x);
}
f();
===
- Rhino (1.6 release 5), Opera (9.23) and Konqueror (3.5.7)
print "function x() { return false; }"

those give the function as the result.
>- Firefox (2.0.0.6) prints "0".

That seems odd to me and looks like a bug in the handling of
the eval call with the function declaration.
That certainly is an odd result. JavaScript(tm) has function statements
in addition to having the function expressions and function declarations
required by ECMA 262, which (not being formally specified themselves)
may account for this outcome. But the string arguments to - eval - are
supposed to be handled as an ECMAScript 'Program' and I don't see how
the contents of the evaled string could be anything but a function
declaration in that context.
>- and I think it should be "global"...

That seems to be the right result if the delete x yielded true.

The specification of the var statement says:
"If the variable statement occurs inside a FunctionDeclaration,
the variables are defined with function-local scope in that
function, as described in section 10.1.3. Otherwise, they are defined
with global scope (that is, they are created
as members of the global object, as described in section
10.1.3) using property attributes { DontDelete }."

One could read that as saying that only a global variable
declaration should create a property with "DontDelete",
One could read it that way in isolation, but taken with section 10.2.3,
where for "Function code" it says "Variable instantiation is performed
using the activation object as the variable object and using property
attributes { DontDelete }", it would be difficult to sustains the
"only".
in that case your expectation of the result of your test
case showing "global" seems right but I a bit doubtful
of my analysis with all implementations seemingly agreeing on setting
"DontDelete" on the local var statement too.
Yes, the DontDelete on the function local variable definition is
required. The question here, and the bug(s) here, are with variable
instantiation for eval. For "Eval Code" ECMS 262 says "variable
instantiation is performed using the calling context's variable object
and using empty property attributes", so no DontDelete. And section
10.1.3 says, for "FunctionDeclaration" that "If the variable object
already ahs a property with this name, replace its value and
attributes." So the - var x - creates an - x - property of the variable
object which does have the DontDelete attribute during variable
instantiation for the execution of the function, but the variable
instantiation for the - eval - call should _replace_ both the value of
that - x - property and its attributes, and the replacement attributes
are empty so the final sate of the variable object's- x - property after
the eval call should be its having no DontDelete attribute. The - x -
property of the variable object should then be deletable so the - delete
x; - expression should delete it (removing the property form the
variable object), and unmask the global - x - variable as a consequence.
Let's hear some other opinions.
There certainly are bugs here, but not particularly important ones.
Using - eval - for its divergent attribute setting does not seem to have
any usefully exploitable applications.

Richard.

Sep 15 '07 #3

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

Similar topics

6
by: Anthony | last post by:
Hi, Can anyone help me out here? I need a construction where a static variable in a derived class is accessible through its base class. The base class is part of a framework. The variable in...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
15
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and...
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
3
by: Mehmet Yavuz S. Soyturk | last post by:
Hello, consider the next code: var obj = {}; with(obj) { var x = 10; } print(x); print(obj.x);
2
by: Bruce | last post by:
From within a class instantiation, I'm trying to set the value of a global variable. Let's call that variable "x". I know I can do this window.x = 5; and it will work but I really need to do...
3
Atli
by: Atli | last post by:
Hi everybody. This is not so much a problem, since I have already managed to find a solution, but my solution requires the use of the eval() function, which I just hate to use. The problem is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.