4

I was playing around with the toString method and when I tried to override a function's toString method, I could see the effect when I tried console.log-ging it. http://jsbin.com/bigocijuqo/1/edit?js,console.

function greet(){
  console.log("Hello!");
}
console.log(greet);

greet.toString = function () {
   return "Overridden toString";
};
console.log(greet);

produces

function greet(){
window.runnerWindow.proxyConsole.log("Hello!");
}
Overridden toString

However, when I tried the same with objects, it doesn't seem to be taking effect. http://jsbin.com/jebunakibi/1/edit?js,console

var ray = {
  'name': 'Ray',
  'fav_food': 'Carrots'
};

console.log(ray);

ray.toString = function () {
  return "My name is " + this.name + " and I loooove " + this.fav_food;
};

console.log(ray);

produces

[object Object] {
  fav_food: "Carrots",
  name: "Ray"
}
[object Object] {
  fav_food: "Carrots",
  name: "Ray",
  toString: function () {
  return "My name is " + this.name + " and I loooove " + this.fav_food;
}
}

I expected the second console.log would print

My name is Ray and I loooove Carrots

I even tried changing the toString method in Object.prototype, but it's still not taking any effect. Can somebody explain what is wrong with this code?

0

1 Answer 1

1

Interesting. Chrome ignores the toString() method in console.log() output.

If you did alert(ray) instead, you would see "My name is Ray and I loooove Carrots."

To see the same thing in the console, do this:

console.log(ray.toString());

… or this:

console.log(ray + '');

You could create a new console function for just this purpose:

console.say= function(s) {
  console.log(s + '');
}

Example:

console.say= function(s) {
  console.log(s + '');
}

var ray = {
  'name': 'Ray',
  'fav_food': 'Carrots'
};

ray.toString = function () {
  return "My name is " + this.name + " and I loooove " + this.fav_food;
};

console.say(ray); //My name is Ray and I loooove Carrots

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, however, I'm a little confused why it's not the same for functions? With greet, when I said console.log(greet); the toString function gets called and whatever that returns is what we see in the console.
Now that's a good question, why it works for functions but not other objects.
I would imagine Chrome uses some internal representation-construction. This allows it to 'browse' objects and to maintain an accurate object graph view, even in the light of modified prototypes. Other browsers like FF and IE will implictly perform a toString, and they require console.dir to get behavior similar to that in Chrome's console.log. Developer tools are developer tools; the behavior is implementation-defined and quirks/differences observed with such should be dismissed from other JavaScript behavior.
@user2864740, I agree, and it's probably a good thing that Chrome acts that way. When debugging, it's often best to see actual data structures. Interesting that Chrome doesn't apply that behavior to functions, however.
@user2864740, I see that in chrome, your example, indeed results in "hi!"!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.