Chaining of methods for Javascript object

method chaining
function Student() {
    this.obj = {};
    this.setName = function(name) {
        this.obj.name = name;
        return this;
    };
    this.setAge = function(age) {
        this.obj.age = age;
        return this;
    };
    this.get = function() {
        console.log(this.obj);
    };
}

let student1 = new Student();
student1
    .setName('pravin')
    .setAge(25)
    .get()

About the Author

Pravin Varma

Tech Lead at Calsoft

You may also like these