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

when to use this?

Howdy All!

Must I *always* precede an instance variable with "this" in JavaScript?

For example, consider the code below:

var initialDate;
function DateControl (initialDate)
{
this.initialDate = initialDate;
}

function doSomething()
{
alert ("typeof initialDate: " + (typeof initialDate));
}

Should initialDate be undefined and this.initialDate be a date?
Or should they both be date?

Thanks for any advice!

Rob
:)
Jul 20 '05 #1
2 1338
"Robert Mark Bram" <re********@remove.this.optusnet.com.au> writes:
Must I *always* precede an instance variable with "this" in JavaScript?
If by an "instance variable" you mean a property of an object, then
you must always[1] access it as part of an object. That object might
be referenced by the "this" keyword.
For example, consider the code below:

var initialDate;
function DateControl (initialDate)
{
this.initialDate = initialDate;
}

function doSomething()
{
alert ("typeof initialDate: " + (typeof initialDate));
} Should initialDate be undefined and this.initialDate be a date?
When this code executes, nothing happens except the creation of two
functions and one global variable.

If you call "doSomething" after this, the scope rules tells us that
"initialDate" inside "doSomething" refers to the global variable.
It is still uninitialized, so the alert reports a type of "undefined".

If you create a new object with "new DateControl(foo)", then during
the execution of the "DateControl" function, the scope rules tells us
that "initialDate" refers to the argument of the "DateControl"
function. When using a function as a constructor, the "this" keyword
refers to the object being created. It then goes on to create a
property on the newly created object. That property is also called
"initialDate", and its value is set to the value of the argument.

If you call "DateControl" as a function (not as a constructor with the
"new" keyword), then inside the body of the function, "this" refers to
the global object. It then sets the property called "initialDate" of the
global object to the value of its argument. The global object contains
the global variables, so this changes the value of the global variable
called "initialDate".
Or should they both be date?


---
var x = 4;
function cx(x) {
this.x=x;
}
var z = new cx("foo");
var y = new cx(true);
cx( function(){var x=42;} );

alert(typeof x + "," + typeof z.x + "," + typeof y.x);
---
What do you expect the alert to say?
Why?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Thank you for the response Lasse!
What do you expect the alert to say?
Why?


I was getting confused and not using "this" when I should have been.

My code had something like:
var initialDate;
function DateControl (initialDate)
{
this.initialDate = initialDate;
this.doSomething = doSomething;
}

function doSomething()
{
alert ("date: " + initialDate);
}

I thought JavaScript might be a bit like Java and assume reference to
"initialDate" in the doSomething() function would default to the instance
variable. Now I have learnt my lesson and changed that type code to use the
"this" reference.

alert ("date: " + this.initialDate);
Rob
:)
Jul 20 '05 #3

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

Similar topics

9
by: Dynamo | last post by:
Hi, I am still confused as when to use single or double quotes. This works: echo "<td>" . $row . "</td>"; and this does not
9
by: Wally | last post by:
I am trying to display images from an Access 2000 database and I get an error "Invalid Parameter Used" when I execute the code line "picBLOB.Image = Image.FromStream(stmBLOBData)" in my Visual...
1
by: nomula_s | last post by:
Can you write a MERGE statement omitting one of the WHEN MATCHED and WHE NOT MACTHED statements. In my program I have to update when macthed and not do any thing when not matched. EX: MERGE...
7
by: cgamache | last post by:
Which is a more correct statement? A destructor runs automatically when an object is deallocated. or A destructor runs automatically immediately before an object is deallocated.
35
by: bonono | last post by:
Hi, I am wondering if there is such a thing, as python is moving away from FP functions like dropwhile/takewhile etc.
2
by: Bob Rock | last post by:
Hello, it is not clear to me when to use new and when to use override (on a virtual method). Could someone explain when to use each. Also it is not clear to me if the hidden or overriden (base)...
1
by: Matt | last post by:
When I try to run the ASP.NET application, it has the following error messages: Error while trying to run project: unable to start debugging on the web server. The project is not configured to...
5
by: news.microsoft.com | last post by:
Hi everyone, I need some help (may be in the form of some sample code) for the subject question. I have an ASP.NET/C# application. I need to do quite a few tasks when the session ends. I...
3
by: Netter | last post by:
I made a usercontrol (a couple of years ago). Actually it is a calculator with lots of little buttons. I have the control on a form. When I view the form in the IDE (or when running) the...
1
by: barrathi | last post by:
Hi all i need one query help! for reporting purpose i wrote one query - follows select s.tran_empid as EmpId, e.M_EMPL_NAME as EmpName, p.PROJ_NAME as Project, t.TITL_NAME as Title,CASE WHEN...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.