Am I using 'this' to often?

G

George

Being nebie in Javascript i am trying to code my own helper object
that does pagination.
So here is a snippet of my code.

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

Without 'this' I get an error 'CheckPageIndex is undefined'. Coming
from object oriented languages like C++ I have a trouble understanding
it.
Or am I doing it wrong and there is a way not to specify 'this' to
many times?
 
L

Lasse Reichstein Nielsen

George said:
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
 
G

George

Being on the same object doesn't affect the scope. You have to use
"this" to access properties of the current object.


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


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.'- Hide quoted text -

- Show quoted text -

Thanks a lot.
George.
 
D

Dr J R Stockton

In comp.lang.javascript message <97de74fe-70a5-422e-be32-033aa2cd0ddf@u2
9g2000pro.googlegroups.com>, Mon, 10 Nov 2008 07:31:20, George
Why do I need to call CheckPageIndex using this.CheckPageIndex when
called from PageUp? It's in the same object...

You can at least approximate to the behaviour elsewhere by using

with (this) { ... ... ... }

although some will consider that unwise.
 

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,135
Messages
2,570,783
Members
47,339
Latest member
flaviu2

Latest Threads

Top