javascriptprototypal-inheritancefunction-binding

Javascript: Why am I able to access Function.prototype.call without mentioning the prototype?


Here is a snippet to explain my question:

+function(str) {
  return str.replace(/^[a-z]|\s[a-z]/g,
            Function.call.bind(String.prototype.toUpperCase));
}('foo bar baz.');  //Returns Foo Bar Baz.

Function.call works, but String.toUpperCase does not. I have to instead write, String.prototype.toUpperCase.


Solution

  • The Function() constructor is itself a function. Therefore, it inherits from the same prototype object as any other function.

    Instances of String() inherit from the prototype, but the String() constructor is not an instance of String(). It too is a function.