The "this" scope is limited to CFCs and allows you to provide properties which can be read and updated by the calling page and by the CFC itself. Though you can use them at any time, they really come into play when the calling page uses the createObject() function to create an instance of your CFC (as opposed to executing a method with the cfinvoke tag). As such, the properties are specific to that instance of the CFC.
Variables declared with the "var" statement, on the other hand, are limited not just to the function, but the specific function call. They are not accessible to, nor can they conflict with variables in the calling page. That way, your function can't overwrite variables in your calling page. Similarly, if the function calls itself recursively, you won't run into conflicts as long as you declare your variables with the "var" statement. In general, always use "var" unless you have a specific reason not to as you will end up with less "odd" or inexplicable behavior. I've always found it odd that scripting languages like ColdFusion and JavaScript don't make the default behavior the most restrictive. In my mind, you should have to go through extra work to expose a value (i.e. by using the "caller" scope in custom tags or the "this" scope in CFCs), not the other way around. I also find it odd that ColdFusion forces developers to place the "var" statements at the beginning of the function. Besides the fact that it seems decidedly "un-ColdFusion-like" to have such seemingly arbitrary restrictions, you end up with variables that are declared no where near where they actually get used. It just makes code harder to read by my way of thinking. Benjamin S. Rogers http://www.c4.net/ v.508.240.0051 f.508.240.0057 -----Original Message----- From: Adrocknaphobia Jones [mailto:[EMAIL PROTECTED]] Sent: Monday, January 06, 2003 4:41 PM To: CF-Talk Subject: RE: CFC - Var vs. This Right. But what's the difference outside of syntax. Would I want to use one over the other? Why would I want to use var over this inside a CF is this is much more flexible? Adam Wayne Lehman Web Systems Developer Johns Hopkins Bloomberg School of Public Health Distance Education Division -----Original Message----- From: Matt Liotta [mailto:[EMAIL PROTECTED]] Sent: Monday, January 06, 2003 4:14 PM To: CF-Talk Subject: RE: CFC - Var vs. This The two are very different. "this" is the public variable scope for a CFC instance. "var" is a keyword used to declare local function variables. "this" can be used anywhere inside a CFC. "var" can only be used immediately proceeding a function declaration or argument specification of a function declaration. Matt Liotta President & CEO Montara Software, Inc. http://www.montarasoftware.com/ 888-408-0900 x901 > -----Original Message----- > From: Adrocknaphobia Jones [mailto:[EMAIL PROTECTED]] > Sent: Monday, January 06, 2003 4:07 PM > To: CF-Talk > Subject: CFC - Var vs. This > > Alright, > > So Christian says to declare all of your private CFC variables at the > top of the function like so <cfset var value = 1>. > Hal Helms recommends using <cfset this.value = 1>. Both obviously work, > but I'm wondering what the pros and cons are if any. > > Off the top of my head, it would seem that 'this' is more functional, as > it acts like any other scope and can be dumped. > > Adam Wayne Lehman > Web Systems Developer > Johns Hopkins Bloomberg School of Public Health > Distance Education Division > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

