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