coldfusion
User defined functions allow developers to keep frequently used pieces of code together in reusable units. For example, you may often find yourself checking the size of a particular file or counting the number of records in a database table. You might find that you need to find out if a string is upper or lowercase, or convert a string to an array of single characters.
用户定义的函数允许开发人员将可重复使用的单元中的常用代码段保持在一起。 例如,您可能经常会发现自己正在检查特定文件的大小或计算数据库表中的记录数。 您可能会发现需要找出字符串是大写还是小写,或者将字符串转换为单个字符的数组。
Whatever your need, user defined functions (UDFs) can help you accomplish your goal. In this article, I’ll show you how to create user defined functions using cfscript so that your function is compatible with both ColdFusion 5 and ColdFusion MX.
无论您有什么需求,用户定义函数(UDF)都可以帮助您实现目标。 在本文中,我将向您展示如何使用cfscript创建用户定义的函数,以便您的函数与ColdFusion 5和ColdFusion MX兼容。
cfscript
函数 (cfscript
Functions)
cfscript has been around since at least version 4.01. It basically allows a developer to write ColdFusion code in C-style syntax. There are limitations to cfscript
, though. For example, you can’t call ColdFusion tags from within cfscript
. You have to use available functions. cfscript
has been proven to offer some performance benefits over the use of tags in some situations, and it often results in your using less code than you may otherwise need. In addition, in ColdFusion 5 you can create your own functions only by using cfscript
. It’s in this context that we’ll use cfscript
to build our own functions that are compatible all the way back to version 5.
cfscript 至少从4.01版本开始存在 。 它基本上允许开发人员以C样式语法编写ColdFusion代码。 但是cfscript
有一些限制。 例如,您不能从cfscript
内调用ColdFusion标签。 您必须使用可用功能。 在某些情况下,事实证明cfscript
可以比使用标签提供更多的性能优势,并且通常会导致您使用的代码少于您可能需要的代码。 此外,在ColdFusion 5中,只能使用cfscript
来创建自己的函数。 在这种情况下,我们将使用cfscript
来构建自己的函数,这些函数一直兼容到版本5。
For our first examples, we’ll use a couple of functions I posted in the ColdFusion blog back in April, which allow us to figure out if a character is uppercase or lowercase. We’ll then add a couple of functions to increase the script’s capabilities in ways that may be useful in the future.
对于第一个示例,我们将使用我在四月份在ColdFusion博客中发布的几个函数,这些函数使我们能够确定字符是大写还是小写。 然后,我们将添加几个函数,以将来可能有用的方式来增加脚本的功能。
大写函数 (The Uppercase Function)
To start, let’s build a function to tell us if a character is uppercase. We’ll then build one to tell us if a character is lowercase. Here’s the code for the function:
首先,让我们构建一个函数来告诉我们字符是否为大写。 然后,我们将构建一个以告诉我们字符是否为小写。 这是该函数的代码:
<cfscript>
function isUpperCase(character) {
if (Asc(character) gte 65 and Asc(character) lte 90)
return true;
return false;
}
</cfscript>
First, we open our cfscript
block. Next, we create a function using the function keyword. We give it a name and then specify, in parentheses, the parameters that it takes. Does this look familiar to you? If you’ve ever worked with JavaScript, it should. This is exactly how a JavaScript function is declared (except for the cfscript
block). There are a few things to note about ColdFusion UDFs, though:
首先,我们打开cfscript
块。 接下来,我们使用function关键字创建一个函数。 我们给它起一个名字,然后在括号中指定它需要的参数。 这看起来对您熟悉吗? 如果您曾经使用过JavaScript,那么应该使用。 正是这样声明JavaScript函数的方式( cfscript
块除外)。 但是,关于ColdFusion UDF,需要注意以下几点:
- You cannot overload or re-declare a function. Once you create it, you cannot create another function of the same name. 您不能重载或重新声明函数。 一旦创建它,就不能创建另一个同名函数。
- You cannot specify default values for parameters in a ColdFusion UDF. 您不能为ColdFusion UDF中的参数指定默认值。
- You cannot specify optional parameters for a UDF. Every parameter you use in the declaration must be passed in the function call. 您不能为UDF指定可选参数。 您在声明中使用的每个参数都必须在函数调用中传递。
Now that I’ve told you what you cannot do with UDFs, here’s something you can do: you can specify parameters that are not declared. For example, I have only declared one parameter for the function above, but I can pass in three, or four, or more parameters. So, a normal function call would look like this:
既然我已经告诉您使用UDF不能执行的操作,则可以执行以下操作:您可以指定未声明的参数。 例如,我只为上面的函数声明了一个参数,但是我可以传入三个,四个或更多参数。 因此,正常的函数调用如下所示:
<cfoutput>
#isUpperCase("A")#
</cfoutput>
This would display the value "true". Note that if I passed in the string "Aaaa" it would still be true, because the Asc
function uses the first character of the string passed in and ignores the rest.
这将显示值“ true”。 请注意,如果我传入字符串“ Aaaa”,它仍然是正确的,因为Asc
函数使用传入的字符串的第一个字符,而忽略其余字符。
However, if I call it with a couple of extra parameters, it will still work:
但是,如果我用几个额外的参数调用它,它将仍然有效:
<cfoutput>
#isUpperCase("A", "a", "b")#
</cfoutput>
This will still display "true". It doesn’t use those additional parameters. But what if we want to use the additional parameters passed into the function? How do we get to them, you ask? It’s really quite simple. Inside the function, we have an array called "arguments". So, if we want to use the second and third parameters, we can simply access them via this array.
这仍将显示“ true”。 它不使用那些附加参数。 但是,如果我们要使用传递给函数的其他参数怎么办? 您问我们如何到达他们? 这真的很简单。 在函数内部,我们有一个称为“参数”的数组。 因此,如果我们要使用第二个和第三个参数,则只需通过此数组即可访问它们。
<cfscript>
function argsTest() {
for (i = 1; i lte ArrayLen(arguments); i = i + 1) {
WriteOutput(arguments[i] & "<br />");
}
}
</cfscript>
This will write out all the arguments that we pass in. We call the function this way:
这将写出所有传入的参数。我们以这种方式调用该函数:
<cfset a = argsTest("I", "am", "ready", "to", "code")>
Using the above code, we’ll get each of those arguments printed on a separate line. Notice a few things about our function here:
使用上面的代码,我们将把每个参数打印在单独的行上。 请注意此处有关我们函数的一些信息:
We use
"lte"
for our comparison. This is because, even though it’s a different syntax, it is still ColdFusion. We must use normal CF operators for comparisons and logic.我们使用
"lte"
进行比较。 这是因为即使语法不同,它仍然是ColdFusion。 我们必须使用常规CF运算符进行比较和逻辑。We use
i = i + 1
instead ofi++
. This is because, again, we’re still using ColdFusion, which does not support the++
operator.我们使用
i = i + 1
代替i++
。 再次因为这是因为我们仍在使用不支持++
运算符的ColdFusion。We use the
WriteOutput()
function to display variables and other information to the page. This is sort of like usingdocument.write()
in a JavaScript function.我们使用
WriteOutput()
函数在页面上显示变量和其他信息。 这有点像在JavaScript函数中使用document.write()
。As in our uppercase function, note that we can use normal ColdFusion functions, such as
Asc(), Chr(), Len(), ArrayLen(), ArrayNew()
, etc., as well.与我们的大写函数一样,请注意,我们也可以使用常规的ColdFusion函数,例如
Asc(), Chr(), Len(), ArrayLen(), ArrayNew()
等。- Notice the return statement in our uppercase function. In this function, we return a simple Boolean, which can then be assigned to any variable we choose. But, we can also return complex values, such as structs and arrays. We’ll see this in just a minute. 注意大写函数中的return语句。 在此函数中,我们返回一个简单的布尔值,然后可以将其分配给我们选择的任何变量。 但是,我们还可以返回复杂的值,例如结构和数组。 我们将在一分钟内看到它。
小写功能 (The Lowercase Function)
Now, let’s code our lowercase function. The ASCII values for our lowercase characters are 97 through 122, so we’ll basically copy and paste the uppercase function and modify those two values.
现在,让我们编写小写的函数。 小写字符的ASCII值为97到122,因此我们基本上将复制并粘贴大写函数并修改这两个值。
<cfscript>
function isLowerCase(character) {
if (Asc(character) lte 97 and Asc(character) gte 122)
return true;
return false;
}
</cfscript>
That will do for those functions. Now, let’s build a function to decide if it is upper or lower case, and alert us to that. We’ll return the value "upper", "lower", or "false" depending on the character passed.
这将对那些功能有用。 现在,让我们构建一个函数来确定它是大写还是小写,并提醒我们注意。 我们将根据传递的字符返回值“ upper”,“ lower”或“ false”。
<cfscript>
function isUpperOrLowerCase(character) {
if (Asc(character) gte 65 and Asc(character) lte 90)
return "upper";
else if (Asc(character) gte 97 and Asc(character) lte 122)
return "lower";
return false;
}
</cfscript>
We can use the function in this way:
我们可以通过这种方式使用该函数:
<cfif not isUpperOrLowerCase("a")>
This is not a character.
<cfelseif isUpperOrLowerCase("a") eq "upper">
This is an uppercase character.
<cfelse>
This is a lowercase character.
</cfif>
This demonstrates the ability to return a data type other than a Boolean from a UDF. Now, let’s return something more complex: an array. We’ll create a function that will turn a string into an array of characters.
这证明了从UDF返回除布尔值以外的数据类型的能力。 现在,让我们返回更复杂的东西:数组。 我们将创建一个将字符串转换为字符数组的函数。
<cfscript>
function toCharacterArray(str) {
var CharArray = ArrayNew(1);
for (i = 1; i lte Len(str); i = i + 1) {
CharArray[i] = Mid(str, i, 1);
}
return CharArray;
}
</cfscript>
This accepts a string as an argument, creates a new, one-dimensional array, loops from 1 to the length of the string, and pulls out each character, stuffing it into the array at the current index, represented by the variable i
. We then return the character array.
这接受一个字符串作为参数,创建一个新的一维数组,从1到字符串的长度循环,然后取出每个字符,将其填充到当前数组中,并以变量i
表示。 然后,我们返回字符数组。
<cfset a = toCharacterArray('This is my array")>
<cfdump var="#a#">
The script above displays a neat little table that shows every value in the array. It really is that easy to create user defined functions in ColdFusion. Now that you know how to create user-defined functions in ColdFusion using cfscript, here’s your assignment:
上面的脚本显示了一个整洁的小表,该表显示了数组中的每个值。 在ColdFusion中创建用户定义的函数真的很容易。 现在您知道如何使用cfscript在ColdFusion中创建用户定义的函数,这是您的任务:
Go through a current project, or a Website that you’ve previously built in ColdFusion, and identify the pieces of that project that could be built into functions. It may be that you use the same functionality in several places, or that you could clean up your code and simplify it by removing it from the body of your templates and putting it into a template. Then, build your functions, store them in a single file (if there are not too many) and simply include them where you need them. You could also create a directory in your site called "udfs", or something like that, and put each function in a file of the same name. Then you simply include the functions you need.
浏览当前项目或您先前在ColdFusion中构建的网站,并确定该项目中可以构建功能的部分。 可能是您在多个地方使用了相同的功能,或者可以通过从模板主体中删除代码并将其放入模板中来清理代码并简化代码。 然后,构建您的函数,将它们存储在一个文件中(如果没有太多),然后仅在需要它们的地方包含它们。 您还可以在站点中创建一个名为“ udfs”或类似名称的目录,然后将每个函数放在同名文件中。 然后,您只需包括所需的功能。
If you want free user defined functions, you may want to look at CFLib – you’ll find tons of very handy functions there.
如果您想要免费的用户定义功能,则可能需要查看CFLib –您会在此处找到大量非常方便的功能。
翻译自: https://www.sitepoint.com/defined-functions-coldfusion/
coldfusion