469,636 Members | 1,714 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Am I using 'this' to often?

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?
Nov 10 '08 #1
3 1134
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.'
Nov 10 '08 #2
On Nov 10, 11:43*am, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
wrote:
George <gev...@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.'- Hide quoted text -

- Show quoted text -
Thanks a lot.
George.
Nov 10 '08 #3
In comp.lang.javascript message <97de74fe-70a5-422e-be32-033aa2cd0ddf@u2
9g2000pro.googlegroups.com>, Mon, 10 Nov 2008 07:31:20, George
<ge****@comcast.netposted:
>
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.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm- also batprogs.htm.
Nov 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

121 posts views Thread by typingcat | last post: by
12 posts views Thread by bj7lewis | last post: by
11 posts views Thread by Brent Ritchie | last post: by
1 post views Thread by Anonieko | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.