473,415 Members | 1,547 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,415 software developers and data experts.

Function Declaration

What is the difference between:

function setupGrid( param ){......}

and

setupGrid = function( param ){......}

Are there any advantages to doing one over the other?

Dec 13 '06 #1
1 1884
INeedADip wrote:
What is the difference between:

function setupGrid( param ){......}

and

setupGrid = function( param ){......}
The point at which function object is created. Other aspects of function
object creation, such as the scope chain, remain the same.

The function object that results from evaluating a function declaration
(the first snippet) is created when the enclosing execution context is
entered, but before code is executed. For function declarations in
global scope, this means that the function object is created before
other global code is executed. For declarations nested within other
functions, the function object is created when the enclosing function is
called.

Function expressions (the second snippet) only create function objects
if they are evaluated, and then only at the point of evaluation.

There are some syntactic differences in that function declarations
cannot occur where just any statement may be used. So, for example, a
function declaration cannot exist as the statement (or within a block)
that's expected after an if statement.

if (...) {
function identifier() {} /* Wrong! */
}

Function expressions aren't constrained in this way, which allows
conditional creation of functions:

var identifier;

if (...) {
identifier = function () {}; /* OK */
}

This also means that a function expression can occur within a with
statement, allowing additional objects to be added to the scope chain.

The identifier in function expressions is also different. With a
function declaration, the identifier that follows the keyword, function,
is the name for a variable that refers to the function object. This
identifier is added to the variable declaration of the execution
context: a local variable within functions, and a global variable at
global scope. With function expressions, the optional identifier is not
exposed outside that function:

var identifier = function hidden() {
alert(typeof hidden); /* function */
};

alert(typeof hidden); /* undefined */

It's just a way of referencing the function from within the function
without resorting to the arguments.callee property.
Are there any advantages to doing one over the other?
Not as such: they're usage differs.

Hope that helps,
Mike
Dec 13 '06 #2

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

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
20
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
20
by: svata | last post by:
Hello there, after some time of pondering I come to some solution which would suit me best. Please correct, if I am wrong. Function has two parameters. A string array, better said a pointer to...
4
by: Paulo Matos | last post by:
Hi all, I'm trying to work out a parser for function declarations but it turns out that it is harder than I initially thought. I'm looking at 3rd Ed of Stroustrup, page 808. I'm trying to parse...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
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
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.