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

Function Format Question!

hi there

i was wondering if someone could give me a brief explanation as to what the
differences are between the following.... i'm guessing that the second
example is more for Object-Oriented programming? Why would it be better
than the first (the inherited code I'm looking at has almost all functions
defined in the second format)

first:

function saveFinal(){

if (confirm("would you like to save your completed chart?")){
window.location="savereport.php";
}

}
versus the second:
saveFinal = function(){

if (confirm("would you like to save your completed chart?")){
window.location="savereport.php";
}

}
Thanks.
Jun 28 '07 #1
4 1310
d d
Good Man wrote:
i was wondering if someone could give me a brief explanation as to what the
differences are between the following.... i'm guessing that the second
example is more for Object-Oriented programming? Why would it be better
than the first (the inherited code I'm looking at has almost all functions
defined in the second format)

first:
function saveFinal(){

if (confirm("would you like to save your completed chart?")){
window.location="savereport.php";
}

}

versus the second:
saveFinal = function(){

if (confirm("would you like to save your completed chart?")){
window.location="savereport.php";
}

}
There is no difference at all. Both create a variable
called saveFinal which is a pointer to that function.

Historically, the first example came first. I'm not
sure at what point (but it was in the 90's) that the
second format became valid.

~dd
Jun 28 '07 #2
On Jun 29, 6:26 am, Good Man <h...@letsgo.comwrote:
hi there

i was wondering if someone could give me a brief explanation as to what the
differences are between the following.... i'm guessing that the second
example is more for Object-Oriented programming? Why would it be better
than the first (the inherited code I'm looking at has almost all functions
defined in the second format)

first:

function saveFinal(){

if (confirm("would you like to save your completed chart?")){
window.location="savereport.php";
}

}

versus the second:

saveFinal = function(){
Always declare variables (use var):

var saveFinal = ...
>
if (confirm("would you like to save your completed chart?")){
window.location="savereport.php";
}
}
The difference between the two is when the function is created. The
first example is a function declaration, so it will be created when
identifiers are resolved and before any code is executed. The second
is a function expression, so the identifier saveFinal will be created
up front (provided var is used) but the function itself won't be
created until the code is executed.

The result is that you can place the code that calls a declared
function before the code that declares it, you can't do that with a
function expression, e.g.

// This is OK
x();
function x() {...}
//This isn't
y(); // --error
var y = function(){...}
Function expressions are frequently used inconjunction with an object
to create a namespace, e.g.

var rX = {};
rX.funcA = function () {...};
rX.funcB = function () {...};

or

var rX = {
funcA: function(){...},
funcb: function(){...}
}

or

var rX = (function(){
// private variables go here
return {
// public variables go here
funcA: function(){...},
funcb: function(){...}
}
})();

and so on. The above add just one variable to the global namespace
(rX), lots of other stuff can be added and not have any clashes
provided rX is OK. It makes the library work well with others -
libraries like jQuery follow this model and (can) add just a single
namespace object (jQuery). Libraries like Prototype.js follow the
model too but create a number of namespaces and so have more chance of
name clashes (which is borne out in practice).

This technique does not make your code object oriented, it just means
you are using an object to create a namespace.
--
Rob

Jun 29 '07 #3
d d
RobG wrote:
On Jun 29, 6:26 am, Good Man <h...@letsgo.comwrote:
>i was wondering if someone could give me a brief
explanation as to what the differences are between
function saveFinal(){
versus the second:
saveFinal = function(){
The difference between the two is when the function is created. The
first example is a function declaration, so it will be created when
identifiers are resolved and before any code is executed. The second
is a function expression, so the identifier saveFinal will be created
up front (provided var is used) but the function itself won't be
created until the code is executed.
Rob
Yeah, just like I said, "quite different".

Or did I say there's no difference at all. Who remembers ;-)

Seriously, thanks for that Rob. I was sure I saw Douglas
Crockford say (in one of his videos) that there's virtually
no difference, but that virtually can be quite significant.

~dd
Jun 29 '07 #4
RobG <rg***@iinet.net.auwrote in
news:11**********************@z28g2000prd.googlegr oups.com:
This technique does not make your code object oriented, it just means
you are using an object to create a namespace.
Fan-freaking-tastic!

Thanks for taking the time to explain.

Thanks to 'd d' as well.
Jun 29 '07 #5

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

Similar topics

1
by: Simon | last post by:
Dear reader, According the manuals it has to be possible to work with "Format" in queries. But in case I use the following expression in a query: Format(;"yyyy-mm-dd") Or...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
5
by: mcbill20 | last post by:
Hello all. I have a really basic question that I hope someone has a better answer for. I apologize in advance-- I know this is probably a really basic question but I am used to Oracle rathern than...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
13
by: Roy Hills | last post by:
I've seen two different function prototype formats used for ANSI C, and I'm unsure as to which is the correct or preferred one. 1st Format (this is what I use) type function(type, type, type);...
2
by: tshad | last post by:
Is there a way to call a function from a boundcolumn tag? I have some icons that I set as visible or not depending on values set in my table. I have a Datagrid that I am binding to and after...
3
by: scorpion53061 | last post by:
Could you look at this function and tell me why I am getting an exception concerning date cast on line set apart by stars... If you have better suggestions of how to do this I would be open to...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
16
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
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
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?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.