469,946 Members | 1,897 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

new foo.prototype.bar(x,y,z ) ?

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

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jan 27 '08 #1
3 1751
On Jan 28, 9:54 am, kj <so...@987jk.com.invalidwrote:
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

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Try:
( new jQuery.prototype.init( selector, context ) ).constructor
== jQuery

and tell me what it says. Specifically, look for the following line of
code for a reference to the init constructors prototype:
jQuery.prototype.init.prototype = jQuery.prototype;
Jan 27 '08 #2
kj
In <97**********************************@q77g2000hsh. googlegroups.com"li*******@gmail.com" <li*******@gmail.comwrites:
>Try:
( new jQuery.prototype.init( selector, context ) ).constructor
== jQuery
>and tell me what it says.
Firebug at least reports that boolean expression as false.
>Specifically, look for the following line of
code for a reference to the init constructors prototype:
jQuery.prototype.init.prototype = jQuery.prototype;
I see, but still, the constructor is not jQuery...

Kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Jan 27 '08 #3
kj <so***@987jk.com.invalidwrites:
In <97**********************************@q77g2000hsh. googlegroups.com"li*******@gmail.com" <li*******@gmail.comwrites:
>>Try:
( new jQuery.prototype.init( selector, context ) ).constructor
== jQuery
>>and tell me what it says.

Firebug at least reports that boolean expression as false.
>>Specifically, look for the following line of
code for a reference to the init constructors prototype:
jQuery.prototype.init.prototype = jQuery.prototype;

I see, but still, the constructor is not jQuery...
As I said in the previous thread, object.constructor is unreliable
unless the person writing the constructor method has taken care that it
works.

You usually don't need it to work anyway, since instanceof works
better.

Joost.
Jan 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Web Developer | last post: by
8 posts views Thread by Elf M. Sternberg | last post: by
13 posts views Thread by eman1000 | last post: by
2 posts views Thread by mithaelin | last post: by
2 posts views Thread by holtmann | last post: by
2 posts views Thread by hzgt9b | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.