Say in JavaScript I create a simple object:
function MyObj() {
this.prop = \"property\";
}
Now if I create an instance of this and the
MyObj.prototype.toString() = function() {} will work, but won't output to the console. If you do something like
console.log("The object says: " + obj);
... you will see the output of toString()
function MyObj() {
this.prop = "property";
}
MyObj.prototype.toString = function() {
return "My property 'prop' has the value: '" + this.prop + "'";
}
var obj = new MyObj();
console.log("the object says: " + obj);
// the object says: My property 'prop' has the value: 'property'
// Or call toString() explicitly
console.log(obj.toString());
// My property 'prop' has the value: 'property'