Search This Blog

2026/08/10

Javascript:Method Chaiining


Implement method chaining.

Code:
function multiply(x) {
    let multiplication = x;

    return {
        multiply(num) {
            multiplication *= num;
            return this;
        },
        getValue() {
            return multiplication;
        }
    };
}

let result = multiply(1).multiply(5).getValue();

console.log(result);

Output:
5

No comments:

Post a Comment