Using a colon with named arguments in ColdFusion

南楼画角 提交于 2019-12-19 02:07:14

问题


I saw this code example in a recording and wanted to know what the colon syntax did. I searched the docs, but I wasn't able to find any info on it:

weather.subscribe(observer: application.observers.currentConditions);

I know we can use colon in CF9 for ternary operators:

result = (condition) ? true : false;

But in this case it looks like it's being used to provide named arguments; so what's it doing there?


回答1:


<cfset result = obj.func(arg:value,thing:42) /> I looked at this and went blink, blink... That can't be right! You can't use colons for named arguments! Er, can you? Well, apparently you can.

http://corfield.org/blog/post.cfm/Learn_something_new_every_day_named_arguments




回答2:


Yes, you are allowed to use both. I think it's a matter of preference. You can even mix.

Try this and see, mocked up some test function:

<cffunction name="testFunction" returntype="void" hint="I just spit out the arguments I get">
    <cfdump var="#arguments#" label="arguments">
</cffunction>

<cfset testFunction(arg1:"hello",arg2:"world") />
<cfset testFunction(arg1="hello",arg2="world") />
<cfset testFunction(arg1:"I can mix",arg2="my named argument syntax") /> 

Personally, I prefer = for named arguments. You might also notice that if you use IntelliJ IDEA for your ColdFusion development that they do not recognize the colon syntax, so for better parsing you would want to use the = syntax. I can't speak for other IDEs




回答3:


Looks like a typo to me. In ColdFusion you would use an equals sign (=) not a colon to used named arguments.

Your example would become:

weather.subscribe(observer = application.observers.currentConditions);


来源:https://stackoverflow.com/questions/6653677/using-a-colon-with-named-arguments-in-coldfusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!