473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1233
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Robin Lawrie | last post by:
Hi again, another problem! I've moved from an Access database to SQL server and am now having trouble inserting dates and times into seperate fields. I'm using ASP and the code below to get the...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
12
by: bj7lewis | last post by:
I am working on a project I want to add a few files as resource to access(copy them to FS and use) at runtime. So far in VS.NET IDE, I Add Files to the project and set its Build Action to...
1
by: Daniel Walzenbach | last post by:
Hi, does anybody know I can extract a substring of a text with regular expressions. Let’s consider the following text: “Regular expressions are often used to make sure that a string matches a...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
5
by: serge | last post by:
Is it generally or almost always better to have multiple small SPs and functions to return a result set instead of using a single big 1000+ lines SP? I have one SP for example that is 1000+...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
0
NoPeasHear
by: NoPeasHear | last post by:
I don't know what I am doing wrong... I used this tutorial... http://www.permadi.com/tutorial/flashMXQuiz/index.html It works with their quiz.xml file, but when I add an option for multiple...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.