> -----Original Message-----
> From: Dominic Watson [mailto:[EMAIL PROTECTED]
> Sent: Monday, March 17, 2008 7:46 PM
> To: CF-Talk
> Subject: Re: UDF function inside cffunction?
> 
>  Javascript allows you to 'nest' functions in as much as you can create
> a
> method for an object on the fly:
> 
> function somefunction(){
>  var someObject = new myObject();
>  someObject.someOtherFunction = function(){alert("I've just been
> created!");}
> }

Calling it a method is really just semantics: functions are data in
JavaScript.  This is really just setting a property whose value is a
function (instead of some other data).

The only thing that really changes for functions in JavaScript is the scope
chain: which scopes they have access and which scopes have access to them.

> I would not say this was a nested function though (which no languages
> allow AFAIK).

While that's not a nested function (again, you're just assigning a function
as data), you can definitely build them in JavaScript:

function somefunction() {
        function myInnerFunction() {alert("I've just been called!");}
        myInnerFunction();
};

In that case myInnerFunction() is a nested function - it's only available
within that function - it's a scoped child.

Unlike your first example (where the function was accessible outside the
parent function because it was assigned to a "public" property) this
function isn't accessible outside.  It won't interfere with global functions
of the same name.

This is a VERY powerful tool for encapsulating functionality that's just not
taken advantage of enough.  How often have you seen excellent JavaScript
libraries with huge footprints (lots of functions and support functions)
that could be condensed into one or two functions if built as nested
functions or object methods?

Throw in recursion, scope escalation, closures and all the other aspects of
scope interaction and you've got the power to do just about anything you
could want.  JavaScript is truly one of the most syntactically powerful and
flexible languages around... it can also be a HUGE pain in the ass for the
same reasons.  ;^)

Jim Davis


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to 
date
Get the Free Trial
http://ad.doubleclick.net/clk;160198600;22374440;w

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:301459
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to