?
Solved

Function definitions in Java Script

Posted on 2012-09-14
2
Medium Priority
?
368 Views
Last Modified: 2012-09-16
Hello experts,

plese have a look at this example:

a.
function XXX() {

}

b.
XXX = function() {

}

What is the difference?
Why are there two of them?

Thank you

panJames
0
Comment
Question by:panJames
2 Comments
 
LVL 8

Accepted Solution

by:
soupBoy earned 1920 total points
ID: 38400234
function xxx(){
//do stuff
}
--- Above is known as a function declaration...

var xxx = function(){
//do stuff
}
--- Above is known as a function expression


When I create objects in Javascript I typically use the function expression, though you can use it for more than just objects ("normal" function usage will work too).

Example:
var myFunction = function(var1){
//some code in here...
}
-- You can call myFunction and pass in a variable by doing:
myFunction(var1);

Take a look at this post: http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/
0
 
LVL 31

Assisted Solution

by:GwynforWeb
GwynforWeb earned 80 total points
ID: 38400479
a.
function XXX() {

}

Is the standard way to define a function


b.
var xxx = function(){
//do stuff
}

allows you to redefine a function definition, (either self defined or DOM methods)

eg

<body onload="document.body.onclick=function() {alert('hello')}">
</body>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question