HighDots Forums  

Basic Question

Javascript JavaScript language (comp.lang.javascript)


Discuss Basic Question in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
levinsontodd@gmail.com
 
Posts: n/a

Default Basic Question - 05-24-2008 , 01:20 AM






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

Discusss.

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

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

Reply With Quote
  #2  
Old   
Stevo
 
Posts: n/a

Default Re: Basic Question - 05-24-2008 , 02:11 AM






levinsontodd (AT) gmail (DOT) com wrote:
Quote:
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"


Reply With Quote
  #3  
Old   
levinsontodd@gmail.com
 
Posts: n/a

Default Re: Basic Question - 05-24-2008 , 09:58 PM



On May 24, 1:08 am, Gregor Kofler <use... (AT) gregorkofler (DOT) at> wrote:
Quote:
levinsont... (AT) gmail (DOT) com 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.


Reply With Quote
  #4  
Old   
david.karr
 
Posts: n/a

Default Re: Basic Question - 05-24-2008 , 11:42 PM



On May 24, 7:58 pm, "levinsont... (AT) gmail (DOT) com" <levinsont... (AT) gmail (DOT) com>
wrote:
Quote:
On May 24, 1:08 am, Gregor Kofler <use... (AT) gregorkofler (DOT) at> wrote:



levinsont... (AT) gmail (DOT) com 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.


Reply With Quote
  #5  
Old   
Evertjan.
 
Posts: n/a

Default Re: Basic Question - 05-25-2008 , 01:55 AM



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

Quote:
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)


Reply With Quote
  #6  
Old   
levinsontodd@gmail.com
 
Posts: n/a

Default Re: Basic Question - 05-25-2008 , 06:25 PM



On May 24, 9:42 pm, "david.karr" <davidmichaelk... (AT) gmail (DOT) com> wrote:
Quote:
On May 24, 7:58 pm, "levinsont... (AT) gmail (DOT) com" <levinsont... (AT) gmail (DOT) com
wrote:



On May 24, 1:08 am, Gregor Kofler <use... (AT) gregorkofler (DOT) at> wrote:

levinsont... (AT) gmail (DOT) com 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.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.