469,923 Members | 1,745 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

document property of object?

Hi,
I've seen javascript code where a constructor function is passed an argument
"document", and inside the function itself the assignment "this.document =
document;" is made. This is the code (or the part necessary for the
example):

function ToggleButton(document) {
ToggleButton.images = new Array(4);
for(i=0;i<4;i++) {
ToggleButton.images[i] = new
Image(ToggleButton.width,ToggleButton.height);
ToggleButton.images[i].src = ToggleButton.imagenames[i];
}
this.document = document;
var index = document.images.length;
}

I don't understand the "this.document = document" part...is it possible to
assign the property of the main window (the global object here) to an
object, in effect making the object equal to the main window? Does anyone
have an explanation for me as to why this works?

Thanks,
Aaron

Jul 20 '05 #1
2 1737
"Aaron" <ag********@comcast.net> wrote in message
news:4a********************@comcast.com...
<snip>
function ToggleButton(document) {
ToggleButton.images = new Array(4);
for(i=0;i<4;i++) {
ToggleButton.images[i] = new
Image(ToggleButton.width,ToggleButton.height);
ToggleButton.images[i].src = ToggleButton.imagenames[i];
}
this.document = document;
var index = document.images.length;
}

I don't understand the "this.document = document" part...is it
possible to assign the property of the main window (the global
object here) to an object,
Yes, the property of the global object with the name "document" is a
_reference_ to the document object. Assigning the value of the global
property with that name to a property of another object from within one
of its methods or its constructor with:-

this.document = document;

- copies the _reference_ to the document object to the property of the
Javascript object. Both properties end up referring to the same object.

In your example constructor function the value assigned is not
necessarily the value of the global - document - property as the
constructor has a formal parameter called "document" and it is whatever
value that parameter holds that is going to be a assigned to -
this.document. If the constructor is called as - new
ToggleButton(document) - then the value held in the global document
property is passed to the constructor as its argument and would be
accessible within the constructor as the parameter - document. But if
(in the context of a frameset) the constructor was called as - new
ToggleButton(parent.document) - then it would be a reference to the
document object belonging to the parent frame, rather than the current
frame, that would be passed and then assigned to - this.document.
in effect making the object equal to the main window?
No, before the assignment the constructed Javascript object has no -
document - property, the assignment creates one and copies the value
held in the - document - parameter to it. The global object is
unaffected, the document object is unaffected and the - this - object
has one extra property that refers to the document object (assuming that
it was that reference that was passed to the constructor as its
argument).

If an assignment did make the - this - object into the object who's
property originally held the value assigned I doubt if the resulting
language would be usable.
Does anyone have an explanation for me as to why this works?


The distinction is between objects, which exist but don't have any
defined or knowable location, and references to objects which may be
assigned to properties of object, local variables and function
parameters (Javascript treats a function's formal parameters as if they
were local variables anyway). The implementation handles the details of
where the objects actually are and how they are referred to, but a
property that refers to an object is holding a value that is a reference
to that object and that value may be passed around, copied, etc in
exactly the same way as a primitive value (such as a number) can.

Richard.
Jul 20 '05 #2
Great explanation, Richard...thanks. I guess I was thinking about
assignment in terms of primitives, not references.

Aaron
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bv*******************@news.demon.co.uk...
"Aaron" <ag********@comcast.net> wrote in message
news:4a********************@comcast.com...
<snip>
function ToggleButton(document) {
ToggleButton.images = new Array(4);
for(i=0;i<4;i++) {
ToggleButton.images[i] = new
Image(ToggleButton.width,ToggleButton.height);
ToggleButton.images[i].src = ToggleButton.imagenames[i];
}
this.document = document;
var index = document.images.length;
}

I don't understand the "this.document = document" part...is it
possible to assign the property of the main window (the global
object here) to an object,


Yes, the property of the global object with the name "document" is a
_reference_ to the document object. Assigning the value of the global
property with that name to a property of another object from within one
of its methods or its constructor with:-

this.document = document;

- copies the _reference_ to the document object to the property of the
Javascript object. Both properties end up referring to the same object.

In your example constructor function the value assigned is not
necessarily the value of the global - document - property as the
constructor has a formal parameter called "document" and it is whatever
value that parameter holds that is going to be a assigned to -
this.document. If the constructor is called as - new
ToggleButton(document) - then the value held in the global document
property is passed to the constructor as its argument and would be
accessible within the constructor as the parameter - document. But if
(in the context of a frameset) the constructor was called as - new
ToggleButton(parent.document) - then it would be a reference to the
document object belonging to the parent frame, rather than the current
frame, that would be passed and then assigned to - this.document.
in effect making the object equal to the main window?


No, before the assignment the constructed Javascript object has no -
document - property, the assignment creates one and copies the value
held in the - document - parameter to it. The global object is
unaffected, the document object is unaffected and the - this - object
has one extra property that refers to the document object (assuming that
it was that reference that was passed to the constructor as its
argument).

If an assignment did make the - this - object into the object who's
property originally held the value assigned I doubt if the resulting
language would be usable.
Does anyone have an explanation for me as to why this works?


The distinction is between objects, which exist but don't have any
defined or knowable location, and references to objects which may be
assigned to properties of object, local variables and function
parameters (Javascript treats a function's formal parameters as if they
were local variables anyway). The implementation handles the details of
where the objects actually are and how they are referred to, but a
property that refers to an object is holding a value that is a reference
to that object and that value may be passed around, copied, etc in
exactly the same way as a primitive value (such as a number) can.

Richard.

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David List | last post: by
5 posts views Thread by James Moe | last post: by
4 posts views Thread by Wow | last post: by
12 posts views Thread by Kepler | last post: by
13 posts views Thread by Stumped and Confused | last post: by
136 posts views Thread by Matt Kruse | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.