Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function Format Question!

0 views
Skip to first unread message

Good Man

unread,
Jun 28, 2007, 4:26:02 PM6/28/07
to
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.

d d

unread,
Jun 28, 2007, 4:30:50 PM6/28/07
to

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

RobG

unread,
Jun 28, 2007, 10:35:52 PM6/28/07
to
On Jun 29, 6:26 am, Good Man <h...@letsgo.com> wrote:
> 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

d d

unread,
Jun 29, 2007, 1:44:31 AM6/29/07
to
RobG wrote:
> On Jun 29, 6:26 am, Good Man <h...@letsgo.com> wrote:
>> 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

Good Man

unread,
Jun 29, 2007, 9:54:46 AM6/29/07
to
RobG <rg...@iinet.net.au> wrote in
news:1183084552.0...@z28g2000prd.googlegroups.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.

0 new messages