From 37a2dc86ce5b9fff7cb96ebae260ba616f1860c2 Mon Sep 17 00:00:00 2001 From: Lakshay Goyal Date: Wed, 15 Apr 2026 18:38:36 +0530 Subject: [PATCH] fix(curriculum): add context for method chaining and this keyword (#66937) --- .../673362cbb475e21eab726506.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/curriculum/challenges/english/blocks/lecture-working-with-higher-order-functions-and-callbacks/673362cbb475e21eab726506.md b/curriculum/challenges/english/blocks/lecture-working-with-higher-order-functions-and-callbacks/673362cbb475e21eab726506.md index e894b8eb0c2..7faab0e80a4 100644 --- a/curriculum/challenges/english/blocks/lecture-working-with-higher-order-functions-and-callbacks/673362cbb475e21eab726506.md +++ b/curriculum/challenges/english/blocks/lecture-working-with-higher-order-functions-and-callbacks/673362cbb475e21eab726506.md @@ -61,6 +61,38 @@ While method chaining can make code more concise and readable, it's important to Very long chains can become difficult to debug, as it's not immediately clear which step in the chain might be causing an issue. It's often a good practice to break very long chains into multiple steps for better clarity and easier debugging. +You can also chain methods on an object. In this case, each method returns `this`, which refers to the current object, allowing the next method in the chain to run on it. + +:::interactive_editor + +```js +const calculator = { + total: 0, + add(n) { + this.total += n; + return this; + }, + multiply(n) { + this.total *= n; + return this; + }, + subtract(n) { + this.total -= n; + return this; + }, + getResult() { + return this.total; + } +}; + +const result = calculator.add(5).multiply(2).subtract(3).getResult(); +console.log(result); // 7 +``` + +::: + +This way, each method returns the same object, so you can keep chaining calls one after another. + # --questions-- ## --text--