function literals

Discussion in 'Javascript' started by Robert, Jul 27, 2005.

  1. Robert

    Robert Guest

    Hi,
    I don't really have a problem, but I would like to know what is
    happening when using function literals for events.
    For example:

    var obj = {value: 5};
    var val = obj.value;
    button.onclick = function()
    {
    alert(obj.value);
    alert(val);
    }

    Clicking will display 5 and 5 of course.
    If some script later will do
    obj.value++;
    the clicking will display 6 and 5.

    Could someone tell me how 'obj.value' and 'val' are resolved in the
    function literal?
    I have some ideas... but like to be sure. Because I keep feeling
    insecure when using function literals :)
     
    Robert, Jul 27, 2005
    #1
    1. Advertising

  2. Robert

    Baconbutty Guest

    >var obj={value:5}

    That is an "object" literal, equivalent to:-

    var obj=new Object();
    obj.value=5;

    >var val = obj.value;


    Numbers, Strings and Booleans are generally passed by "value" not by
    "reference", i.e. *copied* rather than *linked to*.

    So as "obj.value" is a Number, "var val=obj.value" *copies* into the
    variable "val", the value of obj.value *at that point* in the
    execution. It does not link "val" to "object.value" in any way. If
    you later update "object.value" it will have no effect on "val".

    Hence if you update object.value later on, val will not change.

    Contrast this with Arrays and Objects, which are passed by "reference".

    E.g.

    var obj = {value: new Array()};
    obj.value[0]=5;
    var val = obj.value; // val contains a reference to Array

    button.onclick = function()
    {
    obj.value[0]++;

    alert(obj.value[0]);
    alert(val[0]);
    }
     
    Baconbutty, Jul 27, 2005
    #2
    1. Advertising

  3. Robert

    Robert Guest

    Baconbutty wrote:
    > Numbers, Strings and Booleans are generally passed by "value" not by
    > "reference", i.e. *copied* rather than *linked to*.


    Unfortunately I did not seem to be clear enough in my question, because
    this I do know.

    I forgot to mention that the next code is executed in another function,
    so that the object 'obj' does not seem to be in the scope when the
    onclick executes.

    function test()
    {
    var obj = {value: 5};
    var val = obj.value;
    button.onclick = function()
    {
    alert(obj.value);
    alert(val);
    }
    }

    So I would like to know when and how the 'obj.value' is resolved.
     
    Robert, Jul 27, 2005
    #3
  4. Robert

    Baconbutty Guest

    You need to research "closures" and "execution contexts".

    My technical knowledge on this is limited, and others will no doubt
    give more complete answers.

    However, in practice:-

    - When test() is run, "obj" will have scope in both test() and the
    anonymous function. Effectively "obj" is a *global* for the anonymous
    function.

    - A "closure" means that *in some way* the anonymous function is able
    to continue to access "obj" (and "val" for that matter).
     
    Baconbutty, Jul 27, 2005
    #4
  5. Robert

    Baconbutty Guest

    Baconbutty, Jul 27, 2005
    #5
  6. On 27/07/2005 11:15, Robert wrote:

    [snip]

    > I forgot to mention that the next code is executed in another function,
    > so that the object 'obj' does not seem to be in the scope when the
    > onclick executes.


    It is, but explaining how it works properly requires a lot of detail.
    Whilst I don't have any problem with that, closures[1] are covered in
    the FAQ notes[2].

    Have a read. If there are problems, just ask, but please be specific,
    including what you do understand so that can be omitted.

    [snipped code]

    Mike


    [1] <URL:http://www.jibbering.com/faq/faq_notes/closures.html>
    [2] <URL:http://www.jibbering.com/faq/faq_notes/faq_notes.html>

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Jul 27, 2005
    #6
  7. Robert

    Robert Guest

    Robert, Jul 27, 2005
    #7
    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. Al Wilkerson

    literals

    Al Wilkerson, Sep 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,610
    Jonathan Allen
    Sep 25, 2004
  2. Replies:
    15
    Views:
    1,343
    Jerry Coffin
    Feb 1, 2005
  3. John Goche
    Replies:
    8
    Views:
    16,505
  4. Siam
    Replies:
    6
    Views:
    285
    Frederick Gotham
    Jul 25, 2006
  5. Oltmans
    Replies:
    2
    Views:
    94
Loading...

Share This Page