I've recently changed from Java backend to JS frontend. We use a backbone like object structure (at least that's what my colleagues told me) and I wondered if there is a way to override toString()
(e.g. should be called from console.log
).
This is what our models looks like
// namespace
var De = {};
De.Ppasler = {};
De.Ppasler.Model = (function () {
/** @constructor */
function Model(param) {
var self = this;
self.public = function() {
// do sth public
console.log("public", private());
};
function private() {
// do sth private
return "private";
}
// this is what I would have done in Java
self.toString = function() {
return "[object Model]";
}
}
return Model;
}());
var model = new De.Ppasler.Model();
model.public();
console.log(model);
Add toString
Model does not work.
I've also tried this one:
Model.prototype.toString
and self.prototype.toString
but this leads to Errors
I can't make sure model
is defined and I want to avoid an undefined
-check, before calling toString
directly for loggin purpose.