Function
is the global object which all functions use, just like how Number
is the global object for Numbers. They can be used as constructors but also apply to literals, i.e. new Function();
and function () {}
both relate to Function
.
The prototype
Object of a constructor is the place where you define properties and methods for all instances made by that constructor. Constructors are functions and in JavaScript, functions are objects.
A method is just another name for a function, usually describing one which is a property of an object.
Setting a method on Function.prototype
hence means every function inherits that method.
Therefore, the this
in the method bar
on prototype
of constructor Foo
(Foo.prototype.bar
) is equal to one of
Foo.prototype
if invoked as Foo.prototype.bar()
- An instance of
Foo
if invoked from that instance, e.g. z
in var z = new Foo(); z.bar();
- Whatever you've defined
this
to be using call
, apply
or bind
- The global object if invoked without context, i.e.
var b = Foo.prototype.bar; b();
In your code, this
is expected to be the second of the above, because Number
is a function because it is a constructor, and hence an instance of Function
.