How to create a ‘chaining’ JavaScript API with intermediate ‘fluent’ verbage?

I am creating js sdk, where I am looking for creating user (API)/ user call function like;

var user = new user();

user.message.to("username").get.friendlist(function(data){
    //process data received from callback.
});

Now, I know that method chaining can be done, and I am able to make something like

function User(Name) {
    this.uname = Name;
}

User.prototype = {
    constructor: User,

    MessageTo: function (username) {
        this.uname = username;
        return this;
    },

    getFriendList: function (callback) {
        callback("My list");
    }
}

and I can use it as below after creating object of User();

user.messageTo("username").getFriendList(function(data){
});

But I have no Idea about how to get the method call like what I am looking for as;

user.message.to("username").get.friendlist(function(data){
});

Evan I am not sure if it is possible or not. Any help or pointer with same regards is appreciated.


Source: oop

Leave a Reply