473,486 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1863
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12164
by: Web Developer | last post by:
Hi, Comming from Java, it seems that prototype declarations are like abstract methods. I have not read classes in C++ yet, but prototype declarations are a strange concept. Any comments...
8
3721
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
4
1747
by: lkrubner | last post by:
I'm reading an essay, I think one of Crockford's, and it has this example in it: function Demo() { } Demo.prototype = new Ancestor(); Demo.prototype.foo = function () { } ; Does Ancestor now...
13
2518
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
5
2218
by: Daz | last post by:
Hi everyone. My query is very straight forward (I think). What's the difference between someFunc.blah = function(){ ; } and
2
2365
by: mithaelin | last post by:
ok, I can't get out of this riddle and Google seems can't help me. The problem is that this code doesn't work in iexplorer while it's perfect in other browser (didn't check in safari): <html>...
2
1691
by: jidixuelang | last post by:
As I know,it's not well to extend Object.prototype derictly. In the Prototype(JS Framewoke),there is no extend Object.prototype. It only add some static method for Object class. I want to konw the...
2
2495
by: holtmann | last post by:
Hi, I got a question regarding JSON as datasource- I understand that eval on a JSON String creates the appropriate objects in JS. But I would like to use JSON to supply data to already defined...
2
6443
by: hzgt9b | last post by:
I know how to overwrite a function. Normally this is what I would do: function someFunction() { /* orig definition here */ } //later in the execution stream I would do... someFunction = function...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.