var declarations

Discussion in 'Javascript' started by trunikov, Jul 30, 2006.

  1. trunikov

    trunikov Guest

    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
     
    trunikov, Jul 30, 2006
    #1
    1. Advertising

  2. trunikov

    Evertjan. Guest

    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>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jul 30, 2006
    #2
    1. Advertising

  3. trunikov wrote:
    <snip>
    > 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.
     
    Richard Cornford, Jul 30, 2006
    #3
  4. trunikov

    trunikov Guest

    People, thank you very much for your comprehensive answers. They are
    very helpful.
     
    trunikov, Jul 31, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    377
    rooster575
    Dec 2, 2003
  2. thomson
    Replies:
    10
    Views:
    2,521
    Eliyahu Goldin
    Jun 20, 2005
  3. thomson
    Replies:
    0
    Views:
    400
    thomson
    Jun 20, 2005
  4. Fred
    Replies:
    3
    Views:
    336
    Alf P. Steinbach
    Aug 10, 2003
  5. Alex Hall
    Replies:
    5
    Views:
    245
    Lawrence D'Oliveiro
    May 11, 2010
Loading...

Share This Page