Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / June 2007



Tip: Looking for answers? Try searching our database.

Function Format Question!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Good Man - 28 Jun 2007 21:26 GMT
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 - 28 Jun 2007 21:30 GMT
> 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
[quoted text clipped - 19 lines]
>    
> }

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 - 29 Jun 2007 03:35 GMT
> hi there
>
[quoted text clipped - 17 lines]
>
> 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 - 29 Jun 2007 06:44 GMT
>> i was wondering if someone could give me a brief
>> explanation as to what the differences are between
[quoted text clipped - 8 lines]
> 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 - 29 Jun 2007 14:54 GMT
> 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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.