0

Here are two widely seen code snippets in JavaScript:

Function.prototype.method = function (name, func) {
        this.prototype[name] = func;
        return this;
};

Number.method('integer', function () {
    return Math[this < 0 ? 'ceil' : 'floor'](this);
});

Obviously, the this in the second snippet stands for the Number object that's calling the augmented integer method. How about the this in the first snippet? From the prototype property we can guess it stands for the constructor that's being augmented, but the logic behind the snippet is elusive to me. Can anyone detail an explanation? Thanks.

lcn
  • 2,239
  • 25
  • 41
  • `this` can mean anything, especially when the function was secretly bound with underscore.js's `bind` and `bindAll`, so the answer is never what it seems. [Here](http://www.quirksmode.org/js/this.html) is a high-level reference, however. – Brian Sep 26 '13 at 01:34
  • See my answer to this other question for how "this" behaves in javascript. It's not like most other languages: http://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal/13441628#13441628 – slebetman Sep 26 '13 at 01:56

3 Answers3

2

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.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

In the method method, this will be a reference to the Function object that the method is called for. In the example it will be a reference to the Number function.

As Number is a built in class that you can create using the new keyword, it's actually a function, that's why the Function prototype applies to it.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

The this value inside any function called as a method is always the object the method is being called on. So in Number.method(), it will be the Number constructor.

It behaves in the exact same way when you call a method on any other object, like:

var obj = {
    someMethod : function() { 
        console.log(this);
    }
}
obj.someMethod(); // logs obj

The only difference between the above example and yours is that here the method is found on the object itself, while on your example it's found by looking up the prototype chain.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Thanks for pointing out the `Number.method()` is just a method call and thus the simplest rule for `this` referencing applies. – lcn Sep 26 '13 at 02:30