How to Override JavaScript's Tostring() Function to Provide Meaningful Output for Debugging

Is it possible to override JavaScript's toString() function to provide meaningful output for debugging?

You can override toString in Javascript as well. See example:

function Foo() {}

// toString override added to prototype of Foo class
Foo.prototype.toString = function() {
return "[object Foo]";
}

var f = new Foo();
console.log("" + f); // console displays [object Foo]

Overriding a function's toString() in node exports

You only set things on a function's prototype if you're using the function as a constructor with new. Since you don't appear to be doing that here, you need to set the toString function on the actual function you return.

function fooToString() {
return "string representation of function";
}

var Foo = function(arg) {
// some code here...

var ret = function (a, b, c) {
// return function code...
};
ret.toString = fooToString;
return ret;
};

You could also add the toString function when you actually create the object in the bar function, if you'd prefer to do that.

exports.bar(arg) {
var foo = Foo(arg);
foo.toString = fooToString;
return foo;
};

Is there a way to override an object property to turn it into something special

The way the console shows objects is not specified. However it would be a really bad behaviour if the console would cause side effects when logging, or in other words: if it would call an object's method when executed. So no, what you want to do is not possible, also I don't see any usecase for that.

If you want to enrich the debugging experience, you can still add your own logger:

 const log = (...args) => {
console.log(`It is ${new Date}`, ...args);
};

Can you change console.log() output for objects?

I suppose you could create your own function that explicitly calls toString on its argument and logs it:

const obj = { toString() { return 'foo' } };
const myLog = obj => console.log(obj.toString());

myLog(obj);

Override print representation of a JavaScript array

var a = [1, 2, 3];

// Prints using the default representation
console.log(a);

// Try to override toString
a.toString = function() {
return 'some new representation'
}

// append blank string to invoke toString function
console.log(""+a);

How to return a readable string from an object

What you need is to define your class a and add the function toString in it's definition.

function a(){
var _this = this;
_this.b = 1;
_this.c = 2;
_this.toString = function(){return 'you got 2 values';};
return _this;
}

Now you can call the toString function on a directly:

a().toString(); /*executes the function and returns 'you got 2 values'*/

Or you can instance an object from that class d you can call the inner function:

d = new a(); 
d.toString(); /*returns the same value*/

Add toString to backbone-model-like objects

Most browsers tools provide an intelligent console.log that let you explore objects directly, so there's no need to override toString.

console.log demo with class

Even if you would override toString on your object, most browsers implementation of console.log don't call toString on the passed objects.

Something like alert would do what you expect.

Why overriden toString() is not called in javascript

console.log outputs the literal value to the console - it will not coerce your object to a string and therefore won't execute your toString implementation.

You can force it to output a string like this:

console.log(""+node1);

Example:

DIRECTION = {    NONE : 0,    DIAGONAL: 1,    UP: 2,    LEFT: 3};
var Node = function () { this.direction = DIRECTION.NONE; this.weight = 0;};Node.prototype.toString = function NodeToSting(){ console.log('Is called'); var ret = "this.weight"; return ret;};
(function driver(){ var node1 = new Node(); alert(""+node1); //findLcs("ABCBDAB", "BDCABA");})();


Related Topics



Leave a reply



Submit