Quick actions

cmd+k|ctrl+k

Navigation

Languages

Chaining Functions using "This"

Snippet info

Language

JavaScript

Visibility

public

Author

chaosrock

Created

2017-04-05T03:27:20Z

Updated

2017-04-05T03:29:00Z

var MathObject = function (initialValue) {
    
    this.value = initialValue || 0;

    this.add = function (num) {
        this.value += num;
        return this;
    };
    
    this.sub = function (num) {
        this.value -= num;
        return this;
    };

};

var myMath = new MathObject(10);

console.log(myMath.value);

myMath.add(5);

console.log(myMath.value);

myMath.add(5).add(6).sub(20);

console.log(myMath.value);
INFO