Variable Instantiation

A

Asen Bozhilov

ECMA-262 3 specification 10.1.3 Variable Instantiation subpoint 3
says:

| If there is already a property of the variable object with
| the name of a declared variable, the value of the property and its
attributes are not changed.
| Semantically, this
| step must follow the creation of the FormalParameterList and
FunctionDeclaration properties.
| In particular, if a
| declared variable has the same name as a declared function or formal
parameter, the variable
| declaration does
| not disturb the existing property.

function execCtx(a)
{
var a = Math.random();
return a;
}
execCtx(10); //random value

If i proper understand specification, returned value from `execCtx'
must be 10 in that case, but in my Firefox 3.5.4 returned value is
Number generated from Math.radom();

That is a bug or feature or maybe i miss something?

Thanks for responses.
 
A

Asen Bozhilov

You missed one tiny sentence in 12.2 (Variable statement) ;)

"A variable with an Initialiser is assigned the value of its
AssignmentExpression when the VariableStatement is executed, not when
the variable is created."

Mmmm thank you so much :) That explain everything and code like this:

if (!('a' in this))
{
var a = 10;
}
window.alert(a); //undefined

On entering in global execution context `a' will be created.
('a' in this) will be true because property of Global Object with name
`a' been created on enter in execution context.
`a' value will be ever undefined, because VariableStatement never been
executed.

Thanks again.
 
V

VK

Asen said:
if (!('a' in this))
{
    var a = 10;}

window.alert(a); //undefined

On entering in global execution context `a' will be created.
('a' in this) will be true because property of Global Object with name
`a' been created on enter in execution context.
`a' value will be ever undefined, because VariableStatement never been
executed.

This why in a controllable environment I always have the use of
instantiation over assignment as the first section in each block:

var a = '' // future string holder
var b = 0; // future number holder
var c = false; // future boolean holder
var d = null // future object holder

A little bit of a game of course for a loosely typed language but it
helps in many situations plus makes the code more reading friendly.

Just my my two cents...
 
D

David Mark

This why in a controllable environment I always have the use of
instantiation over assignment as the first section in each block:

 var a = ''     // future string holder
 var b = 0;     // future number holder
 var c = false; // future boolean holder
 var d = null   // future object holder

Wouldn't have expected any less from you.
A little bit of a game of course for a loosely typed language but it
helps in many situations plus makes the code more reading friendly.

Just my my two cents...

Worthless at the current exchange rate. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top