node.js - Why overriden toString() is not called in javascript -
i tried override tostring() found that, overridden function in not getting called @ all.
i have gone through this , this, not able track mistake.
my attempt:
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(); console.log(node1); //findlcs("abcbdab", "bdcaba"); })();
output:
{ direction: 0, weight: 0 }
console.log
outputs literal value console - not coerce object string , therefore won't execute tostring
implementation.
you can force output string 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"); })();
Comments
Post a Comment