473,390 Members | 1,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 1860
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.