473,385 Members | 1,375 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,385 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 1923
"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.