You can pass function references around at will, just like in
javascript. So your assignment, rather than calling the method, just
got a reference to the method. When you tried to output it, it was
converted to it's string representation, which is a class name
followed by the memory address it's stored at.
This allows some neat things to be done. Drop this code in a blank
template and run it. Then take a look at all the crazy stuff that
happens.
<cffunction name="dump"><cfloop collection="#arguments#"
item="i"><cfdump var="#arguments[i]#" /></cfloop></cffunction>
<cfscript>
function compareAsc(a, b) {
if (a LT b) return -1;
else if (a GT b) return 1;
else return 0;
}
function compareDesc(a, b) {
if (a LT b) return 1;
else if (a GT b) return -1;
else return 0;
}
function sort(a, compareFunction) { // our friend, bubble sort
var i = "";
var j = "";
for (i = 1; i LTE arrayLen(a); i = i + 1) {
for (j = 1; j LTE arrayLen(a) - i; j = j + 1) {
// note the dependance on the compare function
// rather than an explicit comparison operation
if (compareFunction(a[j + 1], a[j]) LT 0)
arraySwap(a, j + 1, j);
}
}
return a;
}
dump(compareAsc('alex', 'bill'));
x = compareAsc; // assign a reference to compareAsc
dump(x('alex', 'fred')); // it works like compareAsc
dump(x); // sure enough, x is the compareAsc function
dump(toString(x)); // this is like what you saw
function getArray() { return listToArray("alex,fred,bill,aaron,zach,mel"); }
// lets do some tests
dump(
getArray(), // here's the base array
sort(getArray(), compareAsc), // sort it with compareAsc
sort(getArray(), compareDesc) // sort it with compareDesc
);
</cfscript>
<!--- it's not CFSCRIPT specific either --->
<cfset w = getArray />
<cfset x = compareDesc />
<cfset y = sort />
<cfset z = dump />
<cfset z(y(w(), x)) />
The example I've given is kind of contrived, but imagine if you were
sorting an array of something that doesn't work with CF's built-in
comparisons, such as an array of objects. You could write a function
that does know how to compare them (probably based on one or more
field values), and then use the same sort function to sort the array.
cheers,
barneyb
On Wed, 17 Nov 2004 13:32:54 -0600, Dawson, Michael <[EMAIL PROTECTED]> wrote:
> I have a component with a simple getter, objUser.getIdNumber(). This
> method returns a numeric value.
>
> Silly me, I forgot to type the "()" at the end of the method name. I
> ran the page and received the following text:
>
> [EMAIL PROTECTED]
>
> Is this type of information any use to anyone? Are we closer to having
> property methods than we think or am I still just being silly?
>
> M!ke
> ----------------------------------------------------------
> You are subscribed to cfcdev. To unsubscribe, send an email
> to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> in the message of the email.
>
> CFCDev is run by CFCZone (www.cfczone.org) and supported
> by Mindtool, Corporation (www.mindtool.com).
>
> An archive of the CFCDev list is available at
> [EMAIL PROTECTED]
>
--
Barney Boisvert
[EMAIL PROTECTED]
360.319.6145
http://www.barneyb.com/blog/
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).
An archive of the CFCDev list is available at
[EMAIL PROTECTED]