473,893 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.prototyp e = {
....blablabla.. .
PageUp: function() {
this.iFrom += this.pageSize;
this.CheckPageI ndex();
},

CheckPageIndex: function() {
if( this.iFrom >= this.data.lengt h )
this.iFrom = Math.floor((thi s.data.length - 1)/
this.pageSize) * this.pageSize;

if( this.iFrom < 0 )
this.iFrom = 0;
}
}

Why do I need to call CheckPageIndex using this.CheckPageI ndex 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 1254
George <ge****@comcast .netwrites:
MyData.prototyp e = {
....blablabla.. .
PageUp: function() {
this.iFrom += this.pageSize;
this.CheckPageI ndex();
},

CheckPageIndex: function() {
if( this.iFrom >= this.data.lengt h )
this.iFrom = Math.floor((thi s.data.length - 1)/
this.pageSize) * this.pageSize;

if( this.iFrom < 0 )
this.iFrom = 0;
}
}

Why do I need to call CheckPageIndex using this.CheckPageI ndex 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.f oo = 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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 10 '08 #2
On Nov 10, 11:43*am, Lasse Reichstein Nielsen <lrn.unr...@gma il.com>
wrote:
George <gev...@comcast .netwrites:
MyData.prototyp e = {
*....blablabla. ..
* *PageUp: function() {
* * * * this.iFrom += this.pageSize;
* * * * this.CheckPageI ndex();
* * },
* * CheckPageIndex: function() {
* * * * if( this.iFrom >= this.data.lengt h )
* * * * * * this.iFrom = Math.floor((thi s.data.length - 1)/
this.pageSize) * this.pageSize;
* * * * if( this.iFrom < 0 )
* * * * * * this.iFrom = 0;
* * }
}
Why do I need to call CheckPageIndex using this.CheckPageI ndex 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/rasterTriangleD OM.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.javas cript message <97de74fe-70a5-422e-be32-033aa2cd0ddf@u2
9g2000pro.googl egroups.com>, Mon, 10 Nov 2008 07:31:20, George
<ge****@comcast .netposted:
>
Why do I need to call CheckPageIndex using this.CheckPageI ndex 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.demo n.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demo n.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demo n.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
10334
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 date and time, but my script is erroring. '-- Get login date and time cmdLoginDate = Date() cmdLoginTime = Time()
121
10268
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
19
4127
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 the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
15
3796
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 class. How can I invert these C++ code into pure c code by using struct in C language? Can somebody give me any ideas? thanks. For example, how to conver the following code into pure c code?
12
2076
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 Embedded Resource... Now where should look next... Any help...
1
2061
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 certain pattern.”. I e.g. want to extract everything between “expressions” and “a” which is in the above example “are often used to make sure that”. The unfortunately best I could do was the following piece of code: Dim...
11
2561
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 well as learning C#, I think that it's way overdue for me to start learning C++ Templates (I've been learning it for about 5 years now). I think that adding this type of functionality would be a good exercise to help learn template programming....
5
1531
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+ lines and early analysis of the SP I see it first has 3 big blocks of code separated by IF statements. Then within each IF block of code I see 3-4 UNIONs. UNIONs that means they are all returning the same columns so I am guessing these are prime...
1
2400
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 look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...
0
4588
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 correct answers, the results page always gives me a response of 0. What would I change from the tutorial to have multiple correct responses reflect in the results? <?xml version="1.0"?>
0
9985
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11244
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10927
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7173
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4684
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.