Basic Question

Discussion in 'Javascript' started by levinsontodd@gmail.com, May 24, 2008.

  1. Guest

    Why does this code say "hi" before "hey"?

    Discusss.

    window.onload = function() {
    alert("hey");
    test.hi;
    }

    test = {}
    test.hi = alert("hi");
     
    , May 24, 2008
    #1
    1. Advertisements

  2. Stevo Guest

    wrote:
    > Why does this code say "hi" before "hey"?
    > Discusss.
    >
    > window.onload = function() {
    > alert("hey");
    > test.hi;
    > }
    >
    > test = {}
    > test.hi = alert("hi");


    Because you coded it to say "hi" before "hey". Look at the code above.
    Discuss :)

    The onload event will happen when the browser has (basically) parsed to
    the end of the page and executed all the inline code it finds on the
    way. The sequence ni your case goes like this -

    define a handler for onload
    create a test object
    create a .hi inside test and assign the value undefined, which is what
    alert *returns* when you ask it to display "hi"
     
    Stevo, May 24, 2008
    #2
    1. Advertisements

  3. meinte:

    > test = {}
    > test.hi = alert("hi");


    hi gets the "result" of (window.)alert assigned. And before returning
    this result the alert is being popped up. What you were supposedly
    looking for is

    test.hi = function() { alert("hi"); };

    However in your onload listener this.hi won't do the trick (just like
    now). It must be test.hi()

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
     
    Gregor Kofler, May 24, 2008
    #3
  4. Guest

    On May 24, 1:08 am, Gregor Kofler <> wrote:
    > meinte:
    >
    > > test = {}
    > > test.hi = alert("hi");

    >
    > hi gets the "result" of (window.)alert assigned. And before returning
    > this result the alert is being popped up. What you were supposedly
    > looking for is
    >
    > test.hi = function() { alert("hi"); };
    >
    > However in your onload listener this.hi won't do the trick (just like
    > now). It must be test.hi()
    >
    > Gregor
    >
    > --http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum


    Hi Gregor,

    Thanks for your answer.

    So, instead of assigning a function to test.hi that popped up an
    alert, I was actually assigning window.alert to test.hi?

    I guess I still don't understand why the interpreter executed test.hi
    first.
     
    , May 25, 2008
    #4
  5. david.karr Guest

    On May 24, 7:58 pm, "" <>
    wrote:
    > On May 24, 1:08 am, Gregor Kofler <> wrote:
    >
    >
    >
    > > meinte:

    >
    > > > test = {}
    > > > test.hi = alert("hi");

    >
    > > hi gets the "result" of (window.)alert assigned. And before returning
    > > this result the alert is being popped up. What you were supposedly
    > > looking for is

    >
    > > test.hi = function() { alert("hi"); };

    >
    > > However in your onload listener this.hi won't do the trick (just like
    > > now). It must be test.hi()

    >
    > > Gregor

    >
    > > --http://photo.gregorkofler.at:::Landschafts- und Reisefotografiehttp://web.gregorkofler.com::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum

    >
    > Hi Gregor,
    >
    > Thanks for your answer.
    >
    > So, instead of assigning a function to test.hi that popped up an
    > alert, I was actually assigning window.alert to test.hi?
    >
    > I guess I still don't understand why the interpreter executed test.hi
    > first.


    You didn't assign "window.alert" to "test.hi", you assigned
    'window.alert("hi")'. There's a big difference. You can assign
    functions to variables, but that's not what you did. You assigned the
    RESULT of calling 'window.alert("hi")' to "test.hi". Obviously, at
    the time of the assignment, it actually called 'window.alert("hi")',
    displaying the alert box, and then assigning the (meaningless) result
    of calling that function to the variable "test.hi".

    You saw the "hi" alert before the other alert because the other alert
    only would have executed on completion of window load.
     
    david.karr, May 25, 2008
    #5
  6. Evertjan. Guest

    david.karr wrote on 25 mei 2008 in comp.lang.javascript:

    > alert("hi");


    Using

    alert("hi 1");

    and

    alert("hi 2");

    for the two shouldl enlighten you, OP.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., May 25, 2008
    #6
  7. Guest

    On May 24, 9:42 pm, "david.karr" <> wrote:
    > On May 24, 7:58 pm, "" <>
    > wrote:
    >
    >
    >
    > > On May 24, 1:08 am, Gregor Kofler <> wrote:

    >
    > > > meinte:

    >
    > > > > test = {}
    > > > > test.hi = alert("hi");

    >
    > > > hi gets the "result" of (window.)alert assigned. And before returning
    > > > this result the alert is being popped up. What you were supposedly
    > > > looking for is

    >
    > > > test.hi = function() { alert("hi"); };

    >
    > > > However in your onload listener this.hi won't do the trick (just like
    > > > now). It must be test.hi()

    >
    > > > Gregor

    >
    > > > --http://photo.gregorkofler.at:::Landschafts-und Reisefotografiehttp://web.gregorkofler.com:::meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum

    >
    > > Hi Gregor,

    >
    > > Thanks for your answer.

    >
    > > So, instead of assigning a function to test.hi that popped up an
    > > alert, I was actually assigning window.alert to test.hi?

    >
    > > I guess I still don't understand why the interpreter executed test.hi
    > > first.

    >
    > You didn't assign "window.alert" to "test.hi", you assigned
    > 'window.alert("hi")'. There's a big difference. You can assign
    > functions to variables, but that's not what you did. You assigned the
    > RESULT of calling 'window.alert("hi")' to "test.hi". Obviously, at
    > the time of the assignment, it actually called 'window.alert("hi")',
    > displaying the alert box, and then assigning the (meaningless) result
    > of calling that function to the variable "test.hi".
    >
    > You saw the "hi" alert before the other alert because the other alert
    > only would have executed on completion of window load.


    Thanks David, that clears it up for me.
     
    , May 26, 2008
    #7
    1. Advertisements

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. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    481
    Stefan Duenser
    Dec 8, 2004
  2. Replies:
    1
    Views:
    624
    Eric Smith
    Jul 8, 2005
  3. Engineer
    Replies:
    6
    Views:
    733
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    521
  5. Ark Khasin

    A basic question question about volatile use

    Ark Khasin, Jul 31, 2008, in forum: C Programming
    Replies:
    12
    Views:
    386
    Ark Khasin
    Jul 31, 2008
Loading...

Share This Page