Method Calls on Computed Properties
You'll learn: that using bracket notation to call methods follows the exact same this binding rules as dot notation.
The Core Idea
You already know that when you call a method using dot notation like obj.greet(), this inside greet refers to obj. But what happens when you use bracket notation with a computed property name, like obj['greet']()?
Great news: nothing changes. The this binding works identically.
Whether you write obj.method() or obj['method'](), JavaScript sees both as method calls on the object obj. The brackets are just an alternate syntax for accessing properties—they don't change how this gets bound.
Why This Matters
Sometimes you need to compute which method to call:
const robot = {
name: 'Robo',
walk() {
console.log(this.name + ' is walking');
},
run() {
console.log(this.name + ' is running');
}
};
const action = 'walk'; // computed at runtime
robot[action](); // "Robo is walking"
// 'this' is still robot, just like robot.walk()
The variable action determines which method gets called, but the implicit binding rule still applies—this points to robot because we're calling the method on robot.
Same Rules, Different Syntax
Think of bracket notation as the "long-form" way to access properties. Both robot.walk and robot['walk'] retrieve the same function. Calling either one with () makes it a method call, triggering the same this binding behavior.
Key Takeaway: Bracket notation method calls (obj['method']()) follow identical this binding rules as dot notation (obj.method())—both bind this to the object before the brackets or dot.