Created
February 22, 2012 14:34
-
-
Save bennadel/1885365 to your computer and use it in GitHub Desktop.
ColdFusion 10 Beta - Closures And Components And The THIS Scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- | |
Start a CFScript block. Closures can only be used inside | |
of CFScript due to the syntax required to define them. | |
---> | |
<cfscript> | |
// Create an instance of our new component and access the | |
// closure "Tunnel" that we've tried to create. | |
getter = new Simple().getClosure(); | |
// Access the public and private values. | |
writeOutput( "Public: " & getter( "this", "value" ) ); | |
writeOutput( "<br />" ); | |
writeOutput( "Private: " & getter( "variables", "value" ) ); | |
writeOutput( "<br />" ); | |
writeOutput( "Self: " & getter( "self", "value" ) ); | |
</cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- | |
Start a CFScript block. Closures can only be used inside | |
of CFScript due to the syntax required to define them. | |
---> | |
<cfscript> | |
// Create an instance of our new component and access the | |
// closure "Tunnel" that we've tried to create. | |
getter = new Simple().getClosure(); | |
// Now, let's create out second component, that has not methods. | |
override = new Simple2(); | |
// Store the getter closure IN the override component. We just | |
// want to see if the transfer does anything with the runtime | |
// bindings of scopes. | |
override.getter = getter; | |
// Now, let's try to access the public and private scopes from | |
// the FIRST component when calling the accessor ON the SECOND | |
// component. | |
writeOutput( | |
"Public: " & override.getter( "this", "value" ) | |
); | |
writeOutput( "<br />" ); | |
writeOutput( | |
"Private: " & override.getter( "variables", "value" ) | |
); | |
writeOutput( "<br />" ); | |
writeOutput( | |
"Self: " & override.getter( "self", "value" ) | |
); | |
</cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component | |
output = "false" | |
{ | |
// Before we return our closures, let's set some values in the | |
// public and private scopes. | |
this.value = "I am in the THIS scope!!"; | |
variables.value = "I am in the VARIABLES scope!!"; | |
// I return a closure that attempts to access the THIS and | |
// VARIABLES of the current component. We can test to see how | |
// these are bound at runtime. | |
function getClosure(){ | |
// Define a local version of the "THIS" scope. This is a | |
// common practice in languages like JavaScript where the | |
// THIS scope is dynamically bound at method invocation time. | |
// THIS is treated differently than other variables... is the | |
// same true in ColdFusion?? | |
var self = this; | |
// Create our closure accessor. | |
var getter = function( scope, key ){ | |
// Check the target scope for access. | |
if (scope == "this"){ | |
return( this[ key ] ); | |
} else if (scope == "variables"){ | |
return( variables[ key ] ); | |
} else { | |
return( self[ key ] ); | |
} | |
}; | |
// Return the new closure accessor function. | |
return( getter ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component | |
output = "false" | |
{ | |
// For this test, we'll SET the methods from the outside. | |
// But, we want to see if our local values will override the | |
// closure value bindings. | |
this.value = "I am in the OVERRIDE THIS scope."; | |
variables.value = "I am in the OVERRIDE VARIABLES scope!!"; | |
// GETTER will be injected {here}. | |
} |
Really? That's cool. I'll have to give it a try. I can assure you that I was trying to figure something out. The black/white code is so annoying :)
Yep, I battled with it for a while as well before I discovered that. GitHub needs to get with the program and support our script-based components!
I post these to GitHub using their API. What would be cool would be to have a way to set the file-type programmatically. But it looks like you can only do it via file-extension.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Ben,
I know it's kind of kludgey, but you can get syntax highlighting in your component gists by wrapping them with
<cfscript>
tags. Just FYI :)