Connecting Tech Pros Worldwide Forums | Help | Site Map

When to use "document" and when to use "this"

Bryan
Guest
 
Posts: n/a
#1: Sep 23 '06
Hello all,

Can anyone explain when one should use the "document" object and when
one should use the "this" object?

Also, is the "self" object the same as the "document" or "this" object?


Evertjan.
Guest
 
Posts: n/a
#2: Sep 23 '06

re: When to use "document" and when to use "this"


Bryan wrote on 23 sep 2006 in comp.lang.javascript:
Quote:
Can anyone explain when one should use the "document" object and when
one should use the "this" object?
IMHO:

"window" is the default and top element of the DOM tree.
So: document defaults to window.document.
[However parent.document could mean the framse page.]
"window" is also the general scope of variables.

"this" is the top element of the local DOM scope,
and that could be a <div>, an <a>, etc.
Quote:
Also, is the "self" object the same as the "document" or "this" object?
"self" seems to be used outside the browser and DOM environment,
say in wscript jscript or serverside jscript,
as general scope of variables, where in a browser "window" is used.

===========

<body style='color:red;'>
<div style='color:green;'
onclick='alert("this: "+this.style.color);
alert("document.body: "+document.body.style.color);'
Quote:
>Click me</div>
===========

<script type='text/javascript'>

var v = 1;

function show(){
var v = 'Hello';
alert(v) // Hello
alert(this.v) // 1
alert(window.v) // 1
alert(self.v) // 1
}

show()
</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Michael Winter
Guest
 
Posts: n/a
#3: Sep 23 '06

re: When to use "document" and when to use "this"


Evertjan. wrote:

[snip]
Quote:
"window" is the default and top element of the DOM tree.
In browsers, the window identifier is a reference to the global object.
The root of the document tree is the document object.

[snip]
Quote:
"this" is the top element of the local DOM scope,
and that could be a <div>, an <a>, etc.
The this operator value is determined when entering an execution context
(normally, that means calling a function).

When executing "global" code:

<script type="text/javascript">
/* "global" code */
</script>

the this operator value is a reference to the global object.

this == window; // true

When calling a function as a method of some other object, the this
operator refers to that object:

var object = {
method : function() {
return this;
}
};

object.method() == object; // true

However, when a function is called directly, the this operator once
again refers to the global object:

var func = object.method;

func() == object; // false
func() == window; // true

Typically, event listeners are called by the browser as if they were a
method of the element. As a result, the this operator will refer to that
element. An exception is if the listener was added using MSIE's
attachEvent method; the this operator will refer to the global object.
Quote:
Quote:
>Also, is the "self" object the same as the "document" or "this" object?
>
"self" seems to be used outside the browser and DOM environment, ...
Perhaps, but it is also a property of the window (global) object, and is
a reference to the current window or frame.

[snip]

Mike
Closed Thread