0
sap.designstudio.sdk.DataBuffer.subclass("com.sap.sample.jsondatasource.JsonDataSource", function() {

    var that = this;

    var _hasHeaderRow = false;
    var _hasHeaderColumn = false;
    var _csvfile;

    this.init = function() {
        this.defineDimensions([{
            key: "cols",
            text: "City",
            "axis": "COLUMNS",
            "axis_index": 0
        }, {
            key: "rows",
            text: "Date",
            "axis": "ROWS",
            "axis_index": 0
        }], {
            key: "measures",
            text: "Measures",
            containsMeasures: true,
            members: [{
                "key": "measure",
                "text": "Temprature",
                "scalingFactor": 2,
                "formatString": "0.00 EUR;-0.00 EUR"
            }]
        });
    };

    this.csvfile = function(value) {
        if (value === undefined) {
            return _csvfile;
        } else {
            _csvfile = value;
            return this; **//why we using this here?**
        }
    };

    this.hasHeaderRow = function(value) {
        if (value === undefined) {
            return _hasHeaderRow;
        } else {
            _hasHeaderRow = value;
            return this; **//why we using this here?**
        }
    };

    this.hasHeaderColumn = function(value) {
        if (value === undefined) {
            return _hasHeaderColumn;
        } else {
            _hasHeaderColumn = value;
            return this;  **//why we using this here?**
        }
    };



    this.afterUpdate = function() {
      //
    }
});

I have a doubt on this getter and setter. why we are using this here? what's the purpose instead we can return value right? Please anybody explain clearly. I have updated the entire code. Any suggestion now?

danronmoon
  • 3,814
  • 5
  • 34
  • 56
divakar
  • 1,379
  • 6
  • 15
  • 31
  • 1
    show us the context and an example that uses this in code and what it's trying to achieve. – George Stocker Aug 26 '15 at 17:16
  • 1
    The question looks pretty clear to me. – MinusFour Aug 26 '15 at 17:19
  • It refers to the object instance on which the method is currently being called. more info http://stackoverflow.com/questions/8300844/what-does-return-this-do-within-a-javascript-function – Prashant Valanda Aug 26 '15 at 17:20
  • @MinusFour I disagree. There are numerous reasons why one might want to do that, but without context it's not possible to explain further, there are countless possibilities. – Etheryte Aug 26 '15 at 17:21
  • 1
    Well, I find that there's enough context here. He's asking why would you `return this` on getters and setters. I don't think there are many possibilities for that. – MinusFour Aug 26 '15 at 17:26
  • There really are many possibilities. Just because you can't think of them doesn't mean they don't exist. This is why we all ask for clarification instead of guessing. – takendarkk Aug 26 '15 at 17:28
  • Actually @MinusFour is right!! i just want to know why we doing so? – divakar Aug 26 '15 at 17:57
  • @divakar, most obvious use case would be [Method Chaining](http://schier.co/blog/2013/11/14/method-chaining-in-javascript.html). But there are some other possibilities... – MinusFour Aug 26 '15 at 18:07

1 Answers1

2

This is an example of a "fluent interface".

What's this? For example, if you have a an object of the foo class and every method of this class returns the foo instance, you could write

fooInst.do().some().thing()

By the way, many of the jQuery methods are implemented for usage as fluent interface, so this is quite a familiar concept to JS developers.

In the code example, most methods return the instance only if a value has been passed. In this case, the method can be used either as setter or as getter.

This is also similar to what jQuery does with several methods, for example:

var foo = $('#elem').attr('class'); // returns element's class, CANNOT be chained
$('#elem').attr('class', 'foo'); // sets class + returns instance, CAN be chained
lxg
  • 12,375
  • 12
  • 51
  • 73