473,236 Members | 1,581 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,236 software developers and data experts.

is 'this' always defined for functions?

http://www.frostjedi.com/terra/scripts/demo/this.html

'this' is defined, as I understand it, for event handlers, but why is
it defined, here?

Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>this</title>
</head>

<body>
<script type="text/javascript">
(function() {
if (this) {
alert("test exists");
} else {
alert("test doesn't exist");
}
})();
</script>
</body>
</html>
Aug 26 '08 #1
13 1164
yawnmoth <te*******@yahoo.comwrites:
http://www.frostjedi.com/terra/scripts/demo/this.html

'this' is defined, as I understand it, for event handlers, but why is
it defined, here?

Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>this</title>
</head>

<body>
<script type="text/javascript">
(function() {
if (this) {
alert("test exists");
} else {
alert("test doesn't exist");
}
})();
</script>
</body>
</html>
"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object (the "window" object, ignoring some details).

In method calls on a specific object, "this" refers to the object of
the call (and DOM events act like method calls in that respect).

function bla() {
// do something with "this"
}

bla() // act on the global object

var foo = { bar: bla }

foo.bla() // act on the foo object

var zee = {};

foo.bla.call(z) // act on the zee object
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Aug 26 '08 #2
Joost Diepenmaat <jo***@zeekat.nlwrites:
var zee = {};

foo.bla.call(z) // act on the zee object
that should have been

foo.bla.call(zee) // act on the zee object

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Aug 26 '08 #3
On Aug 26, 4:20*pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
Joost Diepenmaat <jo...@zeekat.nlwrites:
var zee = {};
foo.bla.call(z) // act on the zee object

that should have been

foo.bla.call(zee) // act on the zee object
Thanks - I appreciate it!
Aug 26 '08 #4
Joost Diepenmaat wrote:
"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]
Period. The rest of that paragraph is unnecessary to mention here and
ultimately misleading.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 26 '08 #5
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Joost Diepenmaat wrote:
>"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]

Period. The rest of that paragraph is unnecessary to mention here and
ultimately misleading.
You know, it would have been more informative for anyone following
this thread if you'd quoted the part you objected to, and refered to
the current thread where you're discussing that subject. This kind of
reply is not helping anyone.

Thomas, I respect your knowledge and insight, but I really do not like
your posting style.

For future readers: the window/global object thread starts at
Message-ID: <EK****************@flpi143.ffdc.sbc.com>

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Aug 26 '08 #6
Joost Diepenmaat wrote:
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>Joost Diepenmaat wrote:
>>"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]
Period. The rest of that paragraph is unnecessary to mention here and
ultimately misleading.

You know, it would have been more informative for anyone following
this thread if you'd quoted the part you objected to, and refered to
the current thread where you're discussing that subject. This kind of
reply is not helping anyone.
It would have helped you, would you not have fallen victim to your
self-delusions.
Thomas, I respect your knowledge and insight, but I really do not like
your posting style.
Go on crying. I am not going to repeat myself just to pamper those who
cannot seem to follow this simple rule of Usenet: read before you post.
Score adjusted

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Aug 26 '08 #7
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Joost Diepenmaat wrote:
>Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>>Joost Diepenmaat wrote:
"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]
Period. The rest of that paragraph is unnecessary to mention here and
ultimately misleading.

You know, it would have been more informative for anyone following
this thread if you'd quoted the part you objected to, and refered to
the current thread where you're discussing that subject. This kind of
reply is not helping anyone.

It would have helped you, would you not have fallen victim to your
self-delusions.
>Thomas, I respect your knowledge and insight, but I really do not like
your posting style.

Go on crying. I am not going to repeat myself just to pamper those who
cannot seem to follow this simple rule of Usenet: read before you post.
Score adjusted
Please adjust it a lot lower, and don't bother replying to me.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Aug 26 '08 #8
Joost Diepenmaat wrote:
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>Joost Diepenmaat wrote:
>>Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Joost Diepenmaat wrote:
"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]
Period. The rest of that paragraph is unnecessary to mention here and
ultimately misleading.
You know, it would have been more informative for anyone following
this thread if you'd quoted the part you objected to, and refered to
the current thread where you're discussing that subject. This kind of
reply is not helping anyone.
It would have helped you, would you not have fallen victim to your
self-delusions.
[...]
Score adjusted

Please adjust it a lot lower, and don't bother replying to me.
My apologies, this was uncalled for. While I do not think the thread
reference was necessary (we discussed that only hours ago), my quoting
was certainly suboptimal as was my second followup here.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Aug 27 '08 #9
On Aug 26, 6:19*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Joost Diepenmaat wrote:
"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]

Period. *The rest of that paragraph is unnecessary to mention here and
ultimately misleading.
Do they constitute poor coding practices, or something? I actually
thought it was useful (didn't know about call(), for instance),
although if I should be doing something differently, I'd like to
know...
Aug 27 '08 #10
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Joost Diepenmaat wrote:
>Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
>>Joost Diepenmaat wrote:
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Joost Diepenmaat wrote:
You know, it would have been more informative for anyone following
this thread if you'd quoted the part you objected to, and refered to
the current thread where you're discussing that subject. This kind of
reply is not helping anyone.
It would have helped you, would you not have fallen victim to your
self-delusions.
[...]
Score adjusted

Please adjust it a lot lower, and don't bother replying to me.

My apologies, this was uncalled for. While I do not think the thread
reference was necessary (we discussed that only hours ago), my quoting
was certainly suboptimal as was my second followup here.
Apologies accepted.

I'm looking forward to more constructive (if occasionally heated)
discussions in the future.

Joost.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Aug 27 '08 #11
yawnmoth wrote:
On Aug 26, 6:19 pm, Thomas 'PointedEars' Lahn <PointedE...@web.dewrote:
>Joost Diepenmaat wrote:
>>"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]
Period. The rest of that paragraph is unnecessary to mention here and
ultimately misleading.

Do they constitute poor coding practices, or something?
Not Joosts examples, they are OK (but see below). (It would turn out my
suboptimal quoting was even more misleading than what I was actually
referring to. Sigh. [psf 10.1])
I actually thought it was useful (didn't know about call(), for
instance), although if I should be doing something differently, I'd like
to know...
Function.prototype.call() should be feature-tested before called because it
is not universally supported:

<http://PointedEars.de/es-matrix/#f>
HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Aug 27 '08 #12
yawnmoth <te*******@yahoo.comwrites:
On Aug 26, 6:19*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>Joost Diepenmaat wrote:
"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]

Period. *The rest of that paragraph is unnecessary to mention here and
ultimately misleading.

Do they constitute poor coding practices, or something? I actually
thought it was useful (didn't know about call(), for instance),
although if I should be doing something differently, I'd like to
know...
Thomas has (reasonably justifiable) objections to calling the "window"
object the global object. See the 'Why "window.alert()" over
"alert()"?' thread (I linked to it in this one).

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Aug 27 '08 #13
On Aug 26, 7:46*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
yawnmoth wrote:
On Aug 26, 6:19 pm, Thomas 'PointedEars' Lahn <PointedE...@web.dewrote:
Joost Diepenmaat wrote:
"this" means the object of a method call. in short, whenever it
doens't refer to anything more specific, it refers to the global
object [...]
Period. *The rest of that paragraph is unnecessary to mention here and
ultimately misleading.
Do they constitute poor coding practices, or something?

Not Joosts examples, they are OK (but see below). *(It would turn out my
suboptimal quoting was even more misleading than what I was actually
referring to. *Sigh. [psf 10.1])
I actually thought it was useful (didn't know about call(), for
instance), although if I should be doing something differently, I'd like
to know...

Function.prototype.call() should be feature-tested before called because it
is not universally supported:

<http://PointedEars.de/es-matrix/#f>
Thanks! I'll need to read that "Why "window.alert()" over
"alert()"?" thread Joost mentioned :)

Aug 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes...
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
5
by: Ed Havelaar | last post by:
I have a cool function that I want to use as a default value for a column in my table. But I can't because apparently Access doesn't allow user defined functions in expressions for default values....
7
by: jammie_linux | last post by:
Hi, Please look at the following code. In my opinion, the value of "char *str" in the main function should not change even after calling the function "funct" and that's beacuse the "char *str" in...
18
by: anand | last post by:
*********************************************************************************************************** #include<stdio.h> #include<conio.h> #include<math.h> void main() { double...
4
by: sudhir | last post by:
Q 1. I defined a class with 10 functions . It contains declaration of 5 functions and 5 functions are declared and defined. Is this class is said to a abstract class ? Q 2. Which one is...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
14
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding...
7
by: markscottwright | last post by:
I'm using visual studio 8, and the following code is failing. For the life of me, I can't see what's wrong... void myTest(std::string const& in) { using namespace std; string::const_iterator...
14
by: jl_post | last post by:
Hi, I've heard that if you've declared a variable (such as a double or an int) and not initialize it, then the result of printing out its value is undefined. I've also heard that "undefined...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.