Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,659 Programmers for FREE! Ask your question and get quick answers from experts. There are 867 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Javascript Functions

 
Reply to this topicStart new topic

Javascript Functions

chris.tkd
post 23 Jun, 2008 - 03:38 AM
Post #1


D.I.C Head

Group Icon
Joined: 26 Sep, 2006
Posts: 55



Dream Kudos: 25
My Contributions


Hello,

I was just wondering whats the difference between declaring a javascript function like this

CODE

function myFunction() {
....
};


as opposed to this

CODE

function.name=function() {
...
};


Are they exactly the same?

User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 23 Jun, 2008 - 02:59 PM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,669



Thanked 64 times

Dream Kudos: 1075
My Contributions


Okay, round 2 (IE died on me before I got to post last time)

The first way ou are calling a function requires it to be called manually. So it could only act when myFunction() is called.

The second case is used to assign objects and methods functions. For instacne if you used this code:

CODE
document.onerror = function(msg,url,l){
  txt="There was an error on this page.\n\n";
  txt+="Error: " + msg + "\n";
  txt+="URL: " + url + "\n";
  txt+="Line: " + l + "\n\n";
  txt+="Click OK to continue.\n\n";
  alert(txt);
}


It will alert the error automatically every time an error occures in the JS. (granted this is a little different than the normal implementation of the onerror function, and thus it may not work quite right, but I think you should be able to get the point off of it).

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

chris.tkd
post 1 Jul, 2008 - 03:07 AM
Post #3


D.I.C Head

Group Icon
Joined: 26 Sep, 2006
Posts: 55



Dream Kudos: 25
My Contributions


Just a follow up question to this thread.

If if have:

CODE


myObject = function() {
//super cool method stuff
}


How do i get the "super cool method stuff" to execute?

Is myObject actually an object with a method with no name? Or is it a function?
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 1 Jul, 2008 - 09:40 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,669



Thanked 64 times

Dream Kudos: 1075
My Contributions


you would have to set up myObject as an object like so myObject = []; then do something where you declare a property of myObject as a function. Like so:

CODE
myObject.FUNCTION_NAME = function(){
  //DO SOMETHING;
}


That way you can have multiple functions working inside an object.

Here is more information on using and creating objects:

http://w3schools.com/js/js_objects.asp
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/12/08 03:58AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month