473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Basic Question

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

Discusss.

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

test = {}
test.hi = alert("hi");
Jun 27 '08 #1
6 1036
le**********@gmail.com 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"
Jun 27 '08 #2
le**********@gmail.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 Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jun 27 '08 #3
On May 24, 1:08 am, Gregor Kofler <use...@gregorkofler.atwrote:
levinsont...@gmail.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.
Jun 27 '08 #4
On May 24, 7:58 pm, "levinsont...@gmail.com" <levinsont...@gmail.com>
wrote:
On May 24, 1:08 am, Gregor Kofler <use...@gregorkofler.atwrote:
levinsont...@gmail.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.
Jun 27 '08 #5
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)
Jun 27 '08 #6
On May 24, 9:42 pm, "david.karr" <davidmichaelk...@gmail.comwrote:
On May 24, 7:58 pm, "levinsont...@gmail.com" <levinsont...@gmail.com>
wrote:
On May 24, 1:08 am, Gregor Kofler <use...@gregorkofler.atwrote:
levinsont...@gmail.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.
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.