0

What does this point to in this fiddle?

This copies the core parts of jQuery into a simple fiddle.

How can I test for what this points to?

http://jsfiddle.net/tFhFD/3/

Reference

http://code.jquery.com/jquery-latest.js

  • No, this is absolutely not the *core parts of jQuery*. If you're interested in those, read and understand [this answer](http://stackoverflow.com/q/12143590/1048572) – Bergi Sep 25 '12 at 23:56
  • Holy fire ....that is some crazy stuff...overwriting the constructor!! –  Sep 25 '12 at 23:58
  • Is this how they get `this` to point to `jQuery`? –  Sep 26 '12 at 00:01
  • `this` does never point to `jQuery` (assuming a normal invocation)? – Bergi Sep 26 '12 at 00:05
  • the init prototype points to the jQuery prototype..., the init constructor points to the jQuery constructor....hmmmm –  Sep 26 '12 at 00:13
  • technically it does not...but effectively it does...biggest hack I have seen to date. –  Sep 26 '12 at 00:14

2 Answers2

1
var $A = function (test) {
    return new $A.prototype.init(test);
};
$A.prototype = {
    init: function (test) {
        var a = 'function_var';
        this[0] = a;
        this[1] = arguments[0];
    }
};
document.getElementById('foo').innerHTML = $A('hi_there')[0];
console.debug(jQuery('hi_there'));

this in "this" case points to the instance of the init function.

Calling $A's constructor function returns a new instance of $A's prototype init function.

Alex
  • 34,899
  • 5
  • 77
  • 90
  • but then jQuery would not work...this is copied directly from jQuery code. –  Sep 25 '12 at 23:50
  • sorry check out the source http://code.jquery.com/jquery-latest.js....I mean in their code they use this form... –  Sep 25 '12 at 23:54
  • When you use the `new` keyword with a function, the function will be called with the `this` pointer pointing to a *new* freshly created object. – Peter Sep 25 '12 at 23:55
  • I know my point is that when you use `$()` it points to `jQuery.prototype.init`...which would do nothing except return the dom selection you would not be able to call methods. –  Sep 25 '12 at 23:56
  • my answer is very accurate. calling new on a `function` creates a `new` instance of said function, in this case `init`. what's inaccurate? – Alex Sep 26 '12 at 00:00
  • @Xander In your answer you don't explicitly mention the necessity of using the `new` keyword. Also, maybe it's just the way I think, but I just see constructors as code acting on new objects. – Peter Sep 26 '12 at 00:02
1

Read this introduction into the this keyword. You don't really know what this points to, it depends on the invocation of the function.

However, as the init function is invoked with the new keyword, this will be a new object that inherits from the init.prototype object - an instance of the init constructor function.

To inspect the value of this, use your debugger. You can also console.log(this).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    No, properties on `$` are not jQuery's prototype! – Bergi Sep 26 '12 at 00:21
  • in what case is `constructor` used? –  Sep 26 '12 at 16:23
  • Usually never :-) It is a convenience property which instances inherit from the prototype object, and it points to their ... constructor function! – Bergi Sep 26 '12 at 16:46
  • so by convenience you mean it doesn't do anything ? Why do they set it to jQuery? –  Sep 27 '12 at 11:48
  • It is not used by any native functionality, but there may be custom functions that rely on it to clone instances etc – Bergi Sep 27 '12 at 11:54
  • you mean other user-created functions might reference it for some reason or another ( cloning, etc. )? –  Sep 27 '12 at 11:55