The toString conundrum

Last week, in the course of a technical job interview I was asked this question:

    How to make




The first part, chaining functions, is trivial. As you (should) know, you just need to return the object in each chainable method.

function Counter (numInitial) {
      this.counter = numInitial; = function (howmuch) {
            this.counter += howmuch;
            return this;

      this.minus = function (howmuch) {
            this.counter -= howmuch;
            return this;

To test this class in your browser you just need to instantiate it like this and then call the required methods:

test = new Counter(7)
> Counter {counter: 7, plus: function, minus: function, toString: function}

> Counter {counter: 9, plus: function, minus: function, toString: function}

Of course, the result is not the expected. The tester is asking for the code to return “9”, not the object. Fortunately, there are two methods in the Function() that we can override. These methods are used internally whenever JavaScript need to make a comparison, returning the native value of the Function().

// automatically called to return a String representation of our class

Counter.prototype.toString = function () {
      return this.counter;

Of course, you can define this method inside the original definition of the class. I’m just writing this way for you to copy and paste step by step in your browser’s console.

In any case, by overriding the toString method in our class we should get “9”, since console.log() is returning an String. But it doesn’t. It seems that console.log don’t use .toString or .valueOf in FireFox or Chrome. You should thinks of this as a bug, but if you think of it, the function of the console is to give us accurate information of the objects and variables we pass to it. Converting them would, most of the time, not desirable.

The way to really show that our code is working is to force it to return an String, by converting it before passing it to console.log().

// before overriding toString

console.log( + '')
> [object Object]

// after overriding toString

console.log( + '')
> 9