0

How to write an Object for using this object like below

var cal = new calculator;

cal.add(10).add(20).miniz(2).div(2);

console.log(cal.result()); // result 14
JS-Hero
  • 307
  • 1
  • 7
  • 18
  • `return this;` will be useful in your construction. – Niet the Dark Absol May 21 '16 at 16:08
  • 4
    a constructor would be useful. – Nina Scholz May 21 '16 at 16:08
  • how exactly?? @NinaScholz – JS-Hero May 21 '16 at 16:09
  • 2
    This isn't a *how-to* tutorial service. Question is too broad – charlietfl May 21 '16 at 16:11
  • 1
    You really ought to show us you can create the basics of an object with methods. Then object chaining is simply done by adding a `return this` to every method that you want to be chainable and storing intermediate results in the object's instance data. We don't just write code for you here. We help you solve problems in code you've already tried to write. – jfriend00 May 21 '16 at 16:11
  • [Fluent Interfaces - Method Chaining](http://stackoverflow.com/questions/293353/fluent-interfaces-method-chaining) offers some general advice, ableit with snippets in C#, and Wikipedia's [Fluent Interface: JavaScript](https://en.wikipedia.org/wiki/Fluent_interface#JavaScript) provides an example for JS. – Jonathan Lonowski May 21 '16 at 16:12
  • may be please take a look in javascript prototyping – subash May 21 '16 at 16:14

4 Answers4

3

Here you go, this is one way to do it:

My Example

var calculator = function() {
  this.curr = 0;
  this.add = function(n) {
    this.curr += n;
    return this; // returning this at the end of each method is the key to chaining
  };
  this.miniz = function(n) {
    this.curr -= n;
    return this;
  };
  this.div = function(n) {
    this.curr = this.curr / n;
    return this;
  };
  this.result = function() {
    return this.curr;
  };
};

You need to change the instantiation to this:

var cal = new calculator();
omarjmh
  • 13,632
  • 6
  • 34
  • 42
1

Just to get you started:

function Calculator() {
    var value = 0;
    this.add = function (v) {
        value += v;
        return this;
    };
    this.result = function () {
        return value;
    };
}

var cal = new Calculator;

console.log(cal.add(10).result()); // result 10
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

may be this is will help some what..

var Calc = function(){
   this.value = 0;  
};

Calc.prototype.add = function(val){
    this.value += val;
    return this;
};

then you can use like new Calc().add(100).add(100)

but before make sure understood how prototyping is working,

for ref : a sample

subash
  • 3,116
  • 3
  • 18
  • 22
0
function calculator(){
    this.val = 0;
    this.add = function(num){
        this.val += num;
        return this;
    };

    this.miniz = function(num){
        this.val -= num;
        return this;
    };

    this.div = function(num){
        this.val /= num;
        return this;  
    };

    this.result = function(){
        return this.val;
    };
}
JS-Hero
  • 307
  • 1
  • 7
  • 18