George <ge****@comcast.netwrites:
MyData.prototype = {
....blablabla...
PageUp: function() {
this.iFrom += this.pageSize;
this.CheckPageIndex();
},
CheckPageIndex: function() {
if( this.iFrom >= this.data.length )
this.iFrom = Math.floor((this.data.length - 1)/
this.pageSize) * this.pageSize;
if( this.iFrom < 0 )
this.iFrom = 0;
}
}
Why do I need to call CheckPageIndex using this.CheckPageIndex when
called from PageUp? It's in the same object...
Being on the same object doesn't affect the scope. You have to use
"this" to access properties of the current object.
Without 'this' I get an error 'CheckPageIndex is undefined'. Coming
from object oriented languages like C++ I have a trouble understanding
it.
It's not complex, just primitive.
In C++, if the Foo class has a foo_ property, a method on the Foo class
can refer to it as just "foo_". It can also refer to it as "this->foo_".
Javascript only allows the latter, where "this" is a reference instead
of a pointer, so you write "this.foo".
Or am I doing it wrong and there is a way not to specify 'this' to
many times?
In this case, I don't think so.
If I read the same property a lot of times, I will copy it to a local
variable:
Foo.prototype.foo = function() {
var bar = this.bar;
....bar...bar....bar...;
}
In these examples, you use each property two times, or write it between
reads, and the code is fairly short, so I wouldn't bother doing anything.
/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'