var declarations

T

trunikov

Hi ALL!
I'm a bit new in a javascript and thus have some simple questions which
IMHO are obvious for most members of this group.
It is not clear for me what is difference between two declarations of
variables on level of *.js file.
For example:
// *** BEGIN JS FILE ***

myObj; // this is a global var

var myObj2; // is this a global var too?

// *** END JS FILE ***

Are there differences in declaration of variables above?
AFAIK myObj is a global variable. But what about myObj2. Is myObj2 a
global variable too? In other words is semantic of this two
declarations the same?
Thanks.

Dmitry
 
E

Evertjan.

trunikov wrote on 30 jul 2006 in comp.lang.javascript:
Hi ALL!
I'm a bit new in a javascript and thus have some simple questions which
IMHO are obvious for most members of this group.
It is not clear for me what is difference between two declarations of
variables on level of *.js file.
For example:
// *** BEGIN JS FILE ***

myObj; // this is a global var

this is illegal!

myObj = 7;

would be short for

var myObj = 7;

and that would be short for

var myObj;
myObj = 7;
var myObj2; // is this a global var too?

// *** END JS FILE ***

Are there differences in declaration of variables above?
AFAIK myObj is a global variable. But what about myObj2. Is myObj2 a
global variable too? In other words is semantic of this two
declarations the same?

Example, try this as a html file in IE or other browser:

<script type='text/javascript'>

myObj = 20; // shorthand for "var myObj = 20;"

var myObj2;
myObj2 = 10;

function test() {
myObj2 = 5; // modifies the global variable value
var myObj; // makes a local variable
myObj = 3; // sets the local variable value

alert( myObj ); // shows 3, local variable

alert( self.myObj ); // shows 20, global variable
}

test();

alert( myObj ); // shows 20, global scope

alert( myObj2 ); // shows 5, global scope

</script>
 
R

Richard Cornford

trunikov wrote:
It is not clear for me what is difference between two
declarations of variables on level of *.js file.
For example:
// *** BEGIN JS FILE ***

myObj; // this is a global var

This is not a variable declaration. It is an Expression statement,
consisting of nothing but an Identifier. The statement is executed by
first resolving the Identifier against the scope chain to yield an
instance of the internal Reference type. In the global execution context
the only object on the scope chain will be the global object itself, and
as the global object has no properties named 'myObj' the Identifier will
not correspond with any property of any object on the scope chain and
the resulting Reference type will have a null 'base' object. The final
act in resolving the Expression statement is to call the internal
GetValue function, using the Reference type that was the result of
evaluating the Identifier as its argument.

Whenever the internal GetValue function is called with a Reference type
argument that has a null 'base' object an exception is thrown. Thus this
expression statement results in an error.

It is much more likely that you intended something along the lines of:-

myObj = 'something';

- which is another Expression statement, but this time the expression is
an assignment expression. An assignment expression evaluates the right
hand side of assignment operator (- = -) to a value and then resolves
the left hand side to a reference type, which it then used as an
argument to the internal PutValue function (along with the value, which
will be 'Put'). The Identifier - myObj - still resolves into a Reference
type with a null 'base' object, but when such a Reference type is used
with the internal PutValue function instead of an exception being thrown
a new property is created on the global object and the value is assigned
to that new property.

Following this dynamic creation of a new property of the global object
an subsequent attempts to resolve the identifier - myObj - will no
longer result in a reference type with a null 'base' object as the
global object is alwasy at the end of all scope chains and it now has a
'myObj' property (the Reference type will have the global object
assigned to its 'base' property).

That is unless:-

delete myObj;

- is executed, as - delete - would remove the property of the global
object and so it would not be found in subsequent Identifier resolution.
var myObj2; // is this a global var too?

During Variable Instantiation for any execution context (that is, prior
to the execution of any code for that execution context), for each
Identifier in a Variable Statement (- var VariableDeclaration List; -) a
property is created on the 'Variable' object for that execution context.
In the global execution context the global object is used as the
'Variable' object, so a global variable declaration results in the
creation of a property of the global object.
// *** END JS FILE ***

Are there differences in declaration of variables above?

As both assignment to an undeclared Identifier and a global variable
declaration both result in the creation of a property of the global
object these two actions seem similar. However, they are not identical;
In the two cases the properties of the global object are created at
different times, the first happens with the assignment (whenever that
happens) and second happens prior to the execution of any code in the
global execution context. In addition, in the case of a variable
declaration the property created on the 'Variable' object is internally
marked as - DontDelete -, which means that it cannot subsequently be
removed from that object with the - delete operator.
AFAIK myObj is a global variable. But what about myObj2.
Is myObj2 a global variable too?

Only - myObj2 - is a global variable, but the distinction between a
global variable that is effectively a property of the global object and
a runtime created property of the global object is minimal (only the use
of the - DontDelete - attribute and when the properties are created on
the global object).
In other words is semantic of this two
declarations the same?

No, and there are 'bast practice' and practical reasons for declaring
all variables that are intended to be global in the global execution
context. The practical reason being that browsers like IE create
properties of the global object for any DOM element with an ID
attribute, and many with NAME attributes. Subsequent attempts to assign
to these properties throw exceptions, but these properties are not
created when a javascript global variable has already been created with
the same name. Thus naming collisions that may only apply to some
browsers are avoided by the explicit declarations of global variables.

The 'best practice' reason for declaring all global variables is that it
makes identifying errors in the code easier, as omitting a function
local variable declaration will have any subsequent assignments to what
was intended to be a function local variable creating a property of the
global object. If all variables that are intended to be global are
rigorously declared in the global execution context such errors can
easily be distinguished from code that represent deliberate assignments
to global variables as no global declaration could be fount for the
Identifier that as intended to be local.

Richard.
 
T

trunikov

People, thank you very much for your comprehensive answers. They are
very helpful.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top