tracker issue : CF-3773095

select a category, or use search below
(searches all categories and all time range)
Title:

Please allow use of "var " keyword outside of functions

| View in Tracker

Status/Resolution/Reason: Closed/Won't Fix/

Reporter/Name(from Bugbase): Chris Phillips / Chris Phillips (cf_chris)

Created: 06/09/2014

Components: Language

Versions: 11.0

Failure Type: Enhancement Request

Found In Build/Fixed In Build: CF11_Final /

Priority/Frequency: Trivial / Unknown

Locale/System: English / Windows 7 64-bit

Vote Count: 5

Almost all of our work in CF is in script components. In the rare case I am in a display template and I need a new variable, I inevitably type "var foo =" and get a compiler error. This is lame. Please translate "var foo = 1" outside of functions to "variables.foo = 1". This will essentially make it equivalent to "foo = 1". Which is exactly what I am expecting.

This would be similar to how "var foo = 1" outside of a function in JavaScript would put "foo" into the window scope (making it essentially "foo = 1").

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3773095

External Customer Info:
External Company:  
External Customer Name: cf_chris
External Customer Email:  
External Test Config: My Hardware and Environment details:

Attachments:

Comments:

I disagree with this bit: "var foo = 1" outside of functions to "variables.foo = 1" Because a VARed variable is *not* in the variables scope in the normal sequence of events. I would actively vote AGAINST this idea. However I see a case for being able to have file-local variables, analogous to function-local ones. The variables only survive the for the duration of the file they appear in (not child or sibling includes). -- Adam
Comment by External U.
11974 | June 10, 2014 09:21:28 AM GMT
Adam, I was merely suggesting a behavior exhibited by JavaScript (which CF's "var" implementation seems to take inspiration from). I would be happy if it was template local. Actually that would be perfect. Because then you could use it in the pseudo constructor of CFCs and the variable would be private to the instance. (After typing this, I realized it would work for CFCs the way I ORIGINALLY asked for.) Yeah, so having thought some more about it, I stand by my original request. We can just agree to disagree (or you can agree with me...).
Comment by External U.
11975 | June 12, 2014 12:20:34 AM GMT
I think it would be great for the language to allow VAR outside of CFCs. Template-local would be ideal, but VARIABLES would be fine too. Disallowing VAR declarations just feels arbitrary - allowing it would improve code reuse and consistency.
Vote by External U.
11980 | June 12, 2014 12:44:57 AM GMT
+1 to Sean's analysis. Code reuse and consistency
Vote by External U.
11981 | June 12, 2014 04:09:13 AM GMT
+1 for a VAR'd variable outside of CFC to be template-local
Vote by External U.
11982 | November 16, 2014 01:08:17 AM GMT
to clarify: +1 for a VAR'd variable outside of CFC, and outside of a function, to be template-local
Comment by External U.
11976 | November 16, 2014 01:11:49 AM GMT
+1 to template-local variables via var. Oh, and: "I can't do the equation unless I have all the variables." - Tonk Stark, The Avengers
Vote by External U.
11983 | November 16, 2014 04:40:34 PM GMT
This would introduce lot of confusion as currently there is clear distinction between the scope of variables defined using var inside a function and variables defined outside a function which go into the variables scope.
Comment by Vamseekrishna N.
11977 | August 18, 2015 11:29:33 PM GMT
How does that intrinsically cause confusion? What would the basis of the confusion be?
Comment by External U.
11978 | August 19, 2015 12:29:13 AM GMT
+1 as well for template local vars. This would be VERY useful to have.
Vote by External U.
11984 | October 30, 2015 11:28:30 AM GMT
Hi Vamsee, Could var please be allowed in .cfm to create template-local variables? Example: If bar.cfm has "var myVar = 1", then myVar only exists in bar.cfm. This would actually be useful. Thanks!, -Aaron
Comment by External U.
11979 | December 05, 2015 02:31:29 AM GMT