473,372 Members | 1,033 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,372 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 1034
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.