the "this" question

P

pbd22

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.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.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?
 
D

David Golightly

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.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.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.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
 
R

RobG

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.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.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.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.
 
R

RobG

[...]

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
 
T

Thomas 'PointedEars' Lahn

David said:
[...] 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top