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

the "this" question

Hi.

I am wondering what do I replace the "this"
statement with to get the appropriate container/response.

When I try:

cal.prototype.dates(cal.days[i].day);

I get

"this.cells has no properties"

And, in the function declaration (within which lives 'dates'),
there is:

function MyCal()
{

this.cells = new Array();
...

The thing is (and this is key) I am calling
cal.prototype.dates(cal.days[i].day) from a completely
different script (which references a different part of the
HTML on the page).

How do I refer to the container of the
"this" in the "this.cells has no properties" error?

Sep 19 '07 #1
4 1443
On Sep 19, 2:01 pm, pbd22 <dush...@gmail.comwrote:
Hi.

I am wondering what do I replace the "this"
statement with to get the appropriate container/response.

When I try:

cal.prototype.dates(cal.days[i].day);

I get

"this.cells has no properties"

And, in the function declaration (within which lives 'dates'),
there is:

function MyCal()
{

this.cells = new Array();
...
When you call a method on a function's prototype object, the "this"
keyword (which is a keyword, not a statement) refers to the prototype
object, not to any specific instance. Since it looks like
"this.cells" is assigned in the constructor to MyCal, my guess is
you're using the "prototype" reference when you mean the instance
itself. Try:

cal.dates(cal.days[i].day);

though I have no idea whether this will work, since I know nothing
about the "dates" method, or how you have defined the identifier
"cal".
The thing is (and this is key) I am calling
cal.prototype.dates(cal.days[i].day) from a completely
different script (which references a different part of the
HTML on the page).
Actually, and this will probably surprise you, but that's NOT key -
that should actually have nothing to do with it. All script files in
the same window (or frame) share the same global namespace, aka the
window object, regardless of where their script tags are on the page.
There's no standards-compliant way to associate a specific script with
a specific part of your page - they're all global. Once they're all
loaded onto the page, they might as well be in the same script file.

-David

Sep 19 '07 #2
On Sep 20, 7:01 am, pbd22 <dush...@gmail.comwrote:
Hi.

I am wondering what do I replace the "this"
statement with to get the appropriate container/response.

When I try:

cal.prototype.dates(cal.days[i].day);

I get

"this.cells has no properties"
Presumably, cal is a constructor function (and by convention should
start with a capital letter - Cal). The dates method likely uses the
this keyword expecting it to be a reference to an object constructed
from Cal, e.g.:

function Cal(){
// build a Cal object
}

Cal.prototype.dates = function() {
// instructions for building a Cal object
}

// Create an instance of a Cal object
var aCalObject = new Cal(...);

// Call its dates method
aCalObject.dates(arg0);

When dates is called as a method of someCal, its this keyword is a
reference to aCalObject, which should have the required attributes as
a result of being constructed by Cal (that is up to you to ensure).

And, in the function declaration (within which lives 'dates'),
there is:

function MyCal()
{

this.cells = new Array();
...

The thing is (and this is key) I am calling
cal.prototype.dates(cal.days[i].day) from a completely
different script (which references a different part of the
HTML on the page).
So you are calling cal.prototype.dates directly, in which case its
this keyword will reference the prototype, but the property you are
looking for is likely higher up the scope chain and therefore out of
scope the way you are calling it.

How do I refer to the container of the
"this" in the "this.cells has no properties" error?
In that case, use the call method to pass the appropriate object to
the function:

cal.prototype.dates.call(someObj, cal.days[i].day);
Where someObj is the object that the dates this keyword should
reference. It should have the properties that the method expects it
to have.
--
Rob

Sep 19 '07 #3
On Sep 20, 9:24 am, RobG <rg...@iinet.net.auwrote:
[...]

Correcting a few typos...
Presumably, cal is a constructor function (and by convention should
start with a capital letter - Cal). The dates method likely uses the
this keyword expecting it to be a reference to an object constructed
from Cal, e.g.:

function Cal(){
// build a Cal object
Should be:

// instructions for building a Cal object
}

Cal.prototype.dates = function() {
// instructions for building a Cal object
Should be:

// Specify the dates method
// the this keyword refers to the object
// that dates is called as a method of
}

--
Rob
Sep 20 '07 #4
David Golightly wrote:
[...] All script files in the same window (or frame) share the same
global namespace, aka the window object, regardless of where their
script tags are on the page.
posting = posting.replace(/window(\s+)object/gi, "Global$1Object");
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 20 '07 #5

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

Similar topics

5
by: Michael Stevens | last post by:
Probably the wrong wording but since I'm not a scripter I won't claim to know what I'm talking about. I got this script from www.htmlgoodies.com <script language="JavaScript"> <!--...
3
by: Hodad | last post by:
I would like to adapt, as much as possible, the appearance and color red of the font in this button: <P><CENTER><BUTTON VALUE="SUBMIT"><A...
1
by: Peter King | last post by:
if you assign multiple classes in order to keep your classes generic e.g ..classA { position.absolute; left:5 top:5 height:200 width:800 } ..classB { background-color: red} ..classC {...
9
by: aden | last post by:
I have read the years-old threads on this topic, but I wanted to confirm what they suggest. . . Can the this pointer EVER point to a type different from the class that contains the member...
7
by: Daniel Ervi | last post by:
Hi All, I have a question for the group as I can't seem to come up with any suitable solutions. I'm not that new to programming or C#, but neither am I very fluent yet, so I'd appreciate any...
6
by: Thomas H | last post by:
Hi everyone, I've got a question that's more of a "style" question... Do you guys reference "this" for every object that's inherited from System.Web.UI.Page? For example, when you use the...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
14
by: Alexander Dong Back Kim | last post by:
Dear all, I used to use C++ programming language at all time but moved to C# and Java. Few days ago, I restarted studying about C++ with a very beginner's mind. I wrote a simple class and gcc...
5
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
6
by: babakandme | last post by:
Hi to every body...:D I'm a novice C++ programmer & I've a question, I have the ClassA & in it's constructor, I instantiate ClassB, and I want send "this" pointer """pointer to ClassA""" to the...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.