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