473,326 Members | 2,255 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,326 software developers and data experts.

OOP var foo vs this.foo

ceh
Can anyone explain if there is a difference here?
I'm lost, Thanks.

I have a class that works...

function CXMLHTTP()
{
var xmlhttp;
//this.m_xmlhttp;
this.url = "";
this.m_Response = "";

this.init = init;
this.submit = submit;
this.onReadyStateChange = onReadyStateChange;

alert( "CXMLHTTP Constructor" );

function init()
{
try
{
// Firefox, Opera 8.0+, Safari
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}

function onReadyStateChange()
{
if( xmlhttp.readyState == 4 )
{
this.m_Response = xmlhttp.responseText;
alert( "Response = " + this.m_Response );

}
}

function submit( myurl )
{
alert( "start submit" );
xmlhttp.open("POST", myurl, true);
xmlhttp.send(null);
alert( "end submit" );
}

this.init();
xmlhttp.onreadystatechange = this.onReadyStateChange;
}

But if I change it, as I'd like to, to this
( note that the var xmlhttp is now this.m_xmlhttp )
function CXMLHTTP()
{
//var xmlhttp;
this.m_xmlhttp;
this.url = "";
this.m_Response = "";

this.init = init;
this.submit = submit;
this.onReadyStateChange = onReadyStateChange;

alert( "CXMLHTTP Constructor" );

function init()
{
try
{
// Firefox, Opera 8.0+, Safari
this.m_xmlhttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
this.m_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
this.m_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
}

function onReadyStateChange()
{
if( this.m_xmlhttp.readyState == 4 )
{
this.m_Response = this.m_xmlhttp.responseText;
alert( "Response = " + this.m_Response );

}
}

function submit( myurl )
{
alert( "start submit" );
this.m_xmlhttp.open("POST", myurl, true);
this.m_xmlhttp.send(null);
alert( "end submit" );
}

this.init();
this.m_xmlhttp.onreadystatechange = this.onReadyStateChange;
}

I get these errors.
if( this.m_xmlhttp.readyState == 4 )
for the line in onReadyStateChange

Error: this.m_xmlhttp has no properties
Source File: http://localhost/js/CXMLHTTP.js
Line: 47

Why isn't this working?

Is there something special about the onReadyStateChange callback?
does using var change the scope of the variable for this class
somehow?
It all seems ok, except in the callback function.

Thanks
Nov 23 '07 #1
5 1841
ceh wrote:
[...]
I have a class
No, you don't. http://javascript.crockford.com/javascript.html
that works...

function CXMLHTTP()
{
var xmlhttp;
You declare a variable here.
//this.m_xmlhttp;
This does nothing.
[...]
}

But if I change it, as I'd like to, to this
( note that the var xmlhttp is now this.m_xmlhttp )
No, it is not.
>
function CXMLHTTP()
{
//var xmlhttp;
You are not even declaring the variable here.
this.m_xmlhttp;
This does nothing but to evaluate to `undefined'.
}

I get these errors.
if( this.m_xmlhttp.readyState == 4 )
for the line in onReadyStateChange

Error: this.m_xmlhttp has no properties
Source File: http://localhost/js/CXMLHTTP.js
Line: 47

Why isn't this working?
`undefined', the sole value of the `Undefined' type, has no properties.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Nov 24 '07 #2
ceh
On Nov 24, 3:10 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
ceh wrote:
[...]
I have a class

No, you don't. http://javascript.crockford.com/javascript.html
OK... I have an object...
and... I don't really understand OOP in JS. I will re-read that link

Perhaps I should have written
this.m_xmlhttp = "";
and later set it to xmlhttp _object_ instance.
in fact, why do people use this at all? Those vars will be in scope
regardless correct?

If I can't declare variables via this.x = something, then why do all
my other uses of it work?
this.url = "";
For instance.

Are you suggesting that I always need var foo; ? It seems there is a
lot of code out there that uses the this methodology. If that is the
case, then is this what I'd need?

var xmlHTTP;
xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
// use this one from now on.
this.m_xmlHTTP = xmlHTTP;

Or, just don't declare it until I set it?
this.m_xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");

In any case, it wasn't used until onReadyStateChange and by that time
it should have been initialized to something. This is where I'm
confused.

Thanks for your time.
that works...
function CXMLHTTP()
{
var xmlhttp;

You declare a variable here.
//this.m_xmlhttp;

This does nothing.
[...]
}
But if I change it, as I'd like to, to this
( note that the var xmlhttp is now this.m_xmlhttp )

No, it is not.
function CXMLHTTP()
{
//var xmlhttp;

You are not even declaring the variable here.
this.m_xmlhttp;

This does nothing but to evaluate to `undefined'.
}
I get these errors.
if( this.m_xmlhttp.readyState == 4 )
for the line in onReadyStateChange
Error: this.m_xmlhttp has no properties
Source File:http://localhost/js/CXMLHTTP.js
Line: 47
Why isn't this working?

`undefined', the sole value of the `Undefined' type, has no properties.

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Dec 11 '07 #3
ceh wrote:
Perhaps I should have written
this.m_xmlhttp = "";
Assigning `null' would be more appropriate because ...
and later set it to xmlhttp _object_ instance.
.... you would assign an object _reference_ and `null' is an object value
(the sole value of the Null type, representing "the null, empty, or
non-existent [object] reference"). But since there is no strict typing and
both values are false-values (i.e. type-convert to `false'), it does not
matter much. You could even omit the previous assignment and create the
property only when needed (there might be a bunch of warnings on prior read
access in Gecko-based UAs then, though).

I prefer initializing said properties with `null' instead of with other
false-values because it is intuitive and probably wastes a minimum amount of
memory ("" has String properties, `null' has none).
in fact, why do people use this at all? Those vars will be in scope
regardless correct?
No, this has nothing to do with variables or the scope chain. With the
above you are creating or modifying a *property* of the Activation Object.
That object is the calling object or, in the constructor, the object that
is about to be created.
If I can't declare variables via this.x = something, then why do all
my other uses of it work?
this.url = "";
For instance.
In the global execution context, the Activation Object (which `this' refers
to) is the Global Object. But the Global Object is also the Variable Object
of the global context, and so global variables are also properties of that
object. (In local execution contexts, i.e. in methods, unfortunately there
is no way to refer to the Variable Object.)

See the ECMAScript Language Specification, Edition 3 Final, sections 10.1.6
to 10.1.8, and 10.2.
Are you suggesting that I always need var foo; ?
If you want a *variable* and not just a property, then yes.
It seems there is a lot of code out there that uses the this methodology.
Maybe because of those people doing (explicit) OOP?
If that is the case, then is this what I'd need?

var xmlHTTP;
xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
// use this one from now on.
this.m_xmlHTTP = xmlHTTP;

Or, just don't declare it until I set it?
this.m_xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
Impossible to say without you providing the context in which these
statements are to be executed. The context matters.
[Top posting]
Please reply *below sections of trimmed quotes*, as explained and
recommended in the FAQ and FAQ Notes:

http://jibbering.com/faq/
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Dec 11 '07 #4
Thomas 'PointedEars' Lahn wrote:
ceh wrote:
>Perhaps I should have written
this.m_xmlhttp = "";

Assigning `null' would be more appropriate because ...
<snip>
I prefer initializing said properties with `null' instead
of with other false-values because it is intuitive and
probably wastes a minimum amount of memory ("" has
String properties, `null' has none).
<snip>

On of the advantages of using - null - is that any attempt to employ a
property of the 'object' prior to the reference to the object being
assigned will error, which is what you would want to happen because that
would indicate a bug in the code which needed fixing. While assigning
to, or reading from, a property of an empty string would not error
directly (though code that did so would be likely to error elsewhere,
but probably in a way that made the cause and effect relationship of the
error harder to find.

Still, the undefined value would be as false and also error at attempts
to employ it as an object so maybe initialisation itself is unnecessary,
beyond its making an explicit statement about which properties of the -
this - object in this code were going to be used by the code. In most
cases that initialisation with - null - would be more appropriate on the
prototype of the constructor rather than in the constructor and executed
for each object instantiation.

Richard.

Dec 12 '07 #5
VK <sc**********@yahoo.comwrote:
On Nov 23, 5:13 pm, ceh wrote:
>Is there something special about the onReadyStateChange
callback?

Not onreadystatechange specifically
There is at least one thing that is very specific to the
onreadystatechange methods of XML HTTP request objects, in that at leas
one version of the object does not call the assigned function as a
method of the XML HTTP request object and so the - this - value in the
function does not refer to the XML HTTP request object (hence the
tendency for closures to be employed here to keep the association
between the specific XML HTTP request instance and the function assigned
to - onreadystatechange -).
but about any exit from the
execution context in JavaScript.
What does exiting an execution context (or whatever you mean be 'exit')
have to do with the - this - value?
I call this specification oops
"incontinence of [this]":
Because you don't understand how the specification is absolutely
specific about how the - this - value is determined and you don't
appreciate that no implementations are known to get this wrong.
being called out of the scope chain any
method doesn't remember anymore that it's a method of
a particular object so being executed as a simple
stay-alone function with [this] pointing to window.
<snip>

What to scope chins have to do with the - this - value?

Richard.

Dec 12 '07 #6

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

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Alexander Ross | last post by:
I have a variable ($x) that can have 50 different (string) values. I want to check for 7 of those values and do something based on it ... as I see it I have 2 options: 1) if (($x=="one") ||...
0
by: Dan Foley | last post by:
This script runs fine, but I'd like to know why it's so slow.. Thanks for any help out there on how i can make it faster (it might take up to 5 min to write these 3 export files whith 15 records...
5
by: Lee Redeem | last post by:
Hi there I've created abd uploaded this basic PHP script: <html> <head> <title>PHP Test</title> </head> <body> <H1 align="center">
5
by: christopher vogt | last post by:
Hi, i'm wondering if there is something like $this-> to call a method inside another method of the same class without using the classname in front. I actually use class TEST { function...
6
by: Phil Powell | last post by:
Ok guys, here we go again! SELECT s.nnet_produkt_storrelse_navn FROM nnet_produkt_storrelse s, nnet_produkt_varegruppe v, nnet_storrelse_varegruppe_assoc sv, nnet_produkt p WHERE...
1
by: Michel | last post by:
a site like this http://www.dvdzone2.com/dvd Can you make it in PHP and MySQL within 6 weeks? If so, send me your price 2 a r a (at) p a n d o r a . b e
11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.