Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Function Format Question!

已查看 0 次
跳至第一个未读帖子

Good Man

未读,
2007年6月28日 16:26:022007/6/28
收件人
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

未读,
2007年6月28日 16:30:502007/6/28
收件人

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

未读,
2007年6月28日 22:35:522007/6/28
收件人
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

未读,
2007年6月29日 01:44:312007/6/29
收件人
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

未读,
2007年6月29日 09:54:462007/6/29
收件人
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 个新帖子