472,355 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Similar topics

6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
5
by: James Moe | last post by:
Hello, 1. The document.all property (array?) seems to be a popular IE-only attribute. Other browsers (Opera, Mozilla) do not seem to care for it which, of course, occasionally produces weird...
4
by: Wow | last post by:
when calling a object in an html, should I use self.objectname this.objectname or document.objectname? for example, i have a form called theform and a link called thelink i can call...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
29
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.