K
kj
OK, here's another construct I've run into in the jQuery source
that I can't figure out. It looks like this:
return new jQuery.prototype.init( selector, context );
So basically, as far as I can tell, the returned value has the form
new obj.prototype.method( args );
(In addition, FWIW, the function jQuery.prototype.init has several
explict return statements which, as far as I can tell, all end up
returning "this".)
What I find most confusing here is that when I check the constructor
property of the newly created object I find that it is *not*
jQuery.prototype.init. I.e. the boolean expression
( new jQuery.prototype.init( selector, context ) ).constructor
=== jQuery.prototype.init
evaluates to *false*. Even if I test the value of this.constructor
at a breakpoint right at the top of jQuery.prototype.init, what I
get is Object, not jQuery.prototype.init. Contrast this behavior
with
function Foo () {
alert( this.constructor === Foo );
}
x = new Foo();
In this case the alert dialog will display "true", as one would
expect.
I've tried many different variants of this test and I have never
been able to replicate the situation in which the constructor
property of the this object in the scope of a function that has
been called using the new keyword is anything other than the function
in question. These tests included the case in which the function
explicitly returns the this object. (I even ran some of these
tests in SpiderMonkey running on Linux to rule out the possibility
that what I was seeing was either a Firebug artefact or a quirk in
the Firefox implementation of Javascript. I got the same results.)
This is utterly baffling to me...
If anyone can explain to me what's going on I'd appreciate it!
Thanks!
Kynn
that I can't figure out. It looks like this:
return new jQuery.prototype.init( selector, context );
So basically, as far as I can tell, the returned value has the form
new obj.prototype.method( args );
(In addition, FWIW, the function jQuery.prototype.init has several
explict return statements which, as far as I can tell, all end up
returning "this".)
What I find most confusing here is that when I check the constructor
property of the newly created object I find that it is *not*
jQuery.prototype.init. I.e. the boolean expression
( new jQuery.prototype.init( selector, context ) ).constructor
=== jQuery.prototype.init
evaluates to *false*. Even if I test the value of this.constructor
at a breakpoint right at the top of jQuery.prototype.init, what I
get is Object, not jQuery.prototype.init. Contrast this behavior
with
function Foo () {
alert( this.constructor === Foo );
}
x = new Foo();
In this case the alert dialog will display "true", as one would
expect.
I've tried many different variants of this test and I have never
been able to replicate the situation in which the constructor
property of the this object in the scope of a function that has
been called using the new keyword is anything other than the function
in question. These tests included the case in which the function
explicitly returns the this object. (I even ran some of these
tests in SpiderMonkey running on Linux to rule out the possibility
that what I was seeing was either a Firebug artefact or a quirk in
the Firefox implementation of Javascript. I got the same results.)
This is utterly baffling to me...
If anyone can explain to me what's going on I'd appreciate it!
Thanks!
Kynn