A method is just a function that lives inside a CFC — everything from the Functions module (arguments, return values, scope) applies directly. The one new piece is that a method can read and modify the object's own properties without them being passed in as arguments.
Learning Objectives
After completing this lesson, you'll be able to:
- Write a method that reads and modifies the object's own properties.
- Call one method from another method on the same object.
- Use the return-this pattern to enable method chaining.
A Method Using Its Own Object's Data
component accessors="true" {
property name="items" type="array";
function init() {
variables.items = [];
return this;
}
function addItem(string name, numeric price) {
arrayAppend(variables.items, {name: name, price: price});
}
function getTotal() {
var total = 0;
for (var item in variables.items) {
total += item.price;
}
return total;
}
}<cfcomponent accessors="true">
<cfproperty name="items" type="array">
<cffunction name="init">
<cfset variables.items = []>
<cfreturn this>
</cffunction>
<cffunction name="addItem">
<cfargument name="name" type="string" required="true">
<cfargument name="price" type="numeric" required="true">
<cfset arrayAppend(variables.items, {name: arguments.name, price: arguments.price})>
</cffunction>
<cffunction name="getTotal" returnType="numeric">
<cfset var total = 0>
<cfloop array="#variables.items#" index="item">
<cfset total += item.price>
</cfloop>
<cfreturn total>
</cffunction>
</cfcomponent>addItem and getTotal both operate on variables.items without it being passed in as an argument — that's the whole point of bundling data and behavior together.
Method Chaining: Returning this
A method that returns this (the current object) lets calls be chained together fluently, one after another, instead of writing a separate statement for each call.
Auto-generated setters from accessors="true" return void by default, not this — chainable setters like this have to be written by hand.
Common Beginner Mistakes
Trying to chain the auto-generated setX() methods
user.setName("x").setAge(1) fails because accessor-generated setters return void, not this. Write a custom method that ends in return this if chaining is needed.
Forgetting variables. when reading/writing an object's own property inside a method
Without the variables prefix, an unscoped assignment inside a method falls back to the VARIABLES scope, which happens to work by coincidence here — but explicitly writing variables.x makes the intent clear and avoids confusion with local (var) variables of the same name.
Best Practices
- Explicitly scope a property access as variables.x inside a method, even though it isn't strictly required — it reads clearly and avoids ambiguity with local variables.
- Use method chaining sparingly, for genuinely fluent APIs (like a query builder) — not every method needs to return this.
Interview Questions
What does a method need to return to support chaining?
this — the current object instance — so the next method in the chain can be called directly on the result.
Do accessor-generated setX() methods support chaining by default?
No — they return void by default. A custom method that explicitly returns this is needed for chaining.
Summary
In this lesson, you wrote methods that operate on their own object's properties, and covered the return-this pattern for method chaining.
What's Next?
The next lesson covers the constructor — the init() method, and the exact pseudo-constructor sequence that runs before it.