Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the jQuery Conference 2009 (Cambridge, MA) with:

What Happens When I Return The SUPER Scope From Component Constructor?

By Ben Nadel on
Tags: ColdFusion

There's really no need to ever do this (that I can think of), but the other day it popped into my head - what happens if I return the SUPER scope from a components Init() method? To test this, I set up a base class:

  • <cfcomponent
  • output="false"
  • hint="I am the base component.">
  •  
  • <cffunction
  • name="Foo"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I am the FOO method as defined in the Base class.">
  •  
  • <cfreturn "Base &gt; FOO" />
  • </cffunction>
  •  
  • </cfcomponent>

Then, I created a concrete, extending class that both overrides the Foo() method in the base class as well as providing a new method, Bar():

  • <cfcomponent
  • extends="Base"
  • output="false"
  • hint="I am the concrete class.">
  •  
  •  
  • <cffunction
  • name="Init"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I return an intialized object.">
  •  
  • <!--- Return the SUPER object. --->
  • <cfreturn SUPER />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="Bar"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I am the BAR method as defined in the Concrente class.">
  •  
  • <cfreturn "Concrete &gt; BAR" />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="Foo"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I am the FOO method as overridden in the Concrente class.">
  •  
  • <cfreturn "Concrete &gt; FOO" />
  • </cffunction>
  •  
  • </cfcomponent>

Notice that in the concrete class, the Init() method returns the SUPER class rather than the traditional THIS reference.

Now, let me test the use of and existence of the various methods:

  • <!--- Create the component and initialize. --->
  • <cfset objConcrete = CreateObject( "component", "Concrete" )
  • .Init()
  • />
  •  
  • <!--- Try to invoke overriden method. --->
  • <cftry>
  • #objConcrete.Foo()#<br />
  •  
  • <cfcatch>
  • Foo() Failed.<br />
  • </cfcatch>
  • </cftry>
  •  
  • <!--- Try to invoke new method. --->
  • <cftry>
  • #objConcrete.Bar()#<br />
  •  
  • <cfcatch>
  • Bar() Failed.<br />
  • </cfcatch>
  • </cftry>

When we run the above code, we get the following output:

Base > FOO
Bar() Failed.

From these results, we can gather several pieces of information. Most importantly, returning SUPER returns a completely separate object from the THIS scope. Accompanied by this fact, we can see that the methods defined in concrete class are not available in the SUPER method. The most obvious fact that we can take away from this (which we already new) was that the SUPER object has the SUPER-version of the overridden methods; I say that we already knew this because that is the only reason I have ever used the SUPER class - to call a base method.

Not a very useful post, but I thought I would put it here anyway.



Reader Comments

And, dumping out the VARIABLES scope inside of the Foo() method of the SUPER object appears to dump out the VARIABLES scope of the calling page.

Reply to this Comment

Personally, I like the returntype of the init() constructor to the be name of the class rather than "any. Just speculating here, but I would guess CF wouldn't let you return super if you did that. Otherwise, it's an interesting experiment!

Reply to this Comment

@Ryan,

I have only typed core types, I have never typed Components. I just leave those as Any. But, I bet you are correct - it would dislike me returning the wrong type.

Reply to this Comment

@Ryan,

You are correct. It throws this error:

The value returned from the Init function is not of type Concrete. If the component name is specified as a return type, its possible that a definition file for the component cannot be found or is not accessible.

Reply to this Comment

Try returning SUPER without extending a manually created base. I'm guessing you'll get the base component that all CFC's extend, but I'm curious as to what you'll see...

Probably nothing?

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.