what are the function.toString() methods for?
unstoppablecarl opened this issue · 3 comments
unstoppablecarl commented
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() ?
vincentdesmares commented
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
unstoppablecarl commented
Makes sense. Thanks for taking the time to explain.
nosir commented
@vincentdesmares exactly 👍