468,247 Members | 1,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

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

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?

Sep 23 '06 #1
2 1289
Bryan wrote on 23 sep 2006 in comp.lang.javascript:
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.
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);'
>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)
Sep 23 '06 #2
Evertjan. wrote:

[snip]
"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]
"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.
>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
Sep 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by =?Utf-8?B?QXR1bA==?= | last post: by
5 posts views Thread by Jeremy | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.