nosir/obelisk.js

what are the function.toString() methods for?

unstoppablecarl opened this issue · 3 comments

Each object in obelisk is defined like this:

(function (obelisk) {
    "use strict";

    var Foo, p;
    Foo = function () {
        this.initialize();
    };
    p = Foo.prototype;

    p.initialize = function () {
        return this;
    };

    p.toString = function () {
        return "[Foo]";
    };

    obelisk.Foo = Foo;
}(obelisk));

What is the purpose of the toString() ?

To help when concatenated to strings. Mostly.
To better understand it, run the code you gave in your browser console but replace "obelisk" by "window";

Then type in the console:

var a = new Foo();
a + 'test';

You will see printed "[Foo]test"
For more explanations : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

Makes sense. Thanks for taking the time to explain.

@vincentdesmares exactly 👍