Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > new foo.prototype.bar(x,y,z ) ?

Reply
Thread Tools

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

 
 
kj
Guest
Posts: n/a
 
      01-27-2008






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.
 
Reply With Quote
 
 
 
 
liamgegan@gmail.com
Guest
Posts: n/a
 
      01-27-2008
On Jan 28, 9:54 am, kj <so...@987jk.com.invalid> wrote:
> 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;
 
Reply With Quote
 
 
 
 
kj
Guest
Posts: n/a
 
      01-27-2008
In <9716e3eb-d51b-4c05-ad44-> "" <> writes:

>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.
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      01-27-2008
kj <> writes:

> In <9716e3eb-d51b-4c05-ad44-> "" <> writes:
>
>>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.
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      01-29-2008
In <> Joost Diepenmaat <> writes:

>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.


But there's one situation in which only constructor could help
(though it may not), and that is when one wants to find out the
class of an object. instanceof only helps one test against a
specific class, but what if one wanted to find out an objects class
in the first place?

kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript new-new-new-new-newbee weblinkunlimited@gmail.com Javascript 2 03-11-2008 01:15 AM
2008 new year,2008 new business, 2008 new life, much cheap andbeautiful product will help you yhnetstore@gmail.com Digital Photography 0 01-07-2008 04:57 PM
New computer, New OS, New Wireless Problem :-\ =?Utf-8?B?RGFu?= Wireless Networking 3 07-31-2005 02:11 PM
[Firefox] Use New Tab instead of New Window? paul j Firefox 7 04-07-2005 09:40 PM
Why can not register a new .net passport or a new hotmail account Alick Lv MCSD 1 01-04-2004 06:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57