document property of object?

A

Aaron

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 = new
Image(ToggleButton.width,ToggleButton.height);
ToggleButton.images.src = ToggleButton.imagenames;
}
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
 
R

Richard Cornford

function ToggleButton(document) {
ToggleButton.images = new Array(4);
for(i=0;i<4;i++) {
ToggleButton.images = new
Image(ToggleButton.width,ToggleButton.height);
ToggleButton.images.src = ToggleButton.imagenames;
}
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.
 
A

Aaron

Great explanation, Richard...thanks. I guess I was thinking about
assignment in terms of primitives, not references.

Aaron


Richard Cornford said:
function ToggleButton(document) {
ToggleButton.images = new Array(4);
for(i=0;i<4;i++) {
ToggleButton.images = new
Image(ToggleButton.width,ToggleButton.height);
ToggleButton.images.src = ToggleButton.imagenames;
}
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top