1

I noticed this the other day for the first time while checking out the "CamanJS" project. This question is difficult to explain unless you just do it. If you go to camanjs.com, then using either Firebug or the Chrome JavaScript console, type in the word Caman and press enter. Not Caman() with parentheses -- just Caman. It will output: Version 4.1.0, Released 2/12/2013.

But if you look at typeof Caman, it's just a "function." So how on earth do you get it to output a custom string like that, without actually calling the function? Is that done through some sort of prototype extension?

1
  • 1
    Another interesting "native" property is valueOf: var x = function () {}; x.valueOf = function () {return 5;}; // x + 3 === 8 Commented Feb 22, 2013 at 20:15

1 Answer 1

6

This would happen if the function has a custom toString() method.

For example:

function x() { }
x.toString = function() { return "Hi there!"; };  
console.log(x)
Sign up to request clarification or add additional context in comments.

1 Comment

Interestingly, the return of toString is used as the object's name even if you use console.dir (tested in Chrome)

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.