473,568 Members | 3,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursively calling a function within a class definition

i've got an object and i'd like to recursively call a function
within the class definition.

(i've simplified the code )

function myclass()
{
this.loop = function(index)
{
// ..work

setTimeout( "loop(" + (index+1) + ")", 100 );
}
}

does not work.

i've also tried

setTimeout( function() { loop(index+1) }, 100 );

i've also prefixed this. to loop in both cases - and no workee.

i think it's some sort of scope problem.

Aug 13 '07 #1
1 4801
On Aug 13, 11:50 am, jman <erjdri...@gmai l.comwrote:
i've got an object and i'd like to recursively call a function
within the class definition.

(i've simplified the code )

function myclass()
{
this.loop = function(index)
{
// ..work

setTimeout( "loop(" + (index+1) + ")", 100 );
}

}

does not work.

i've also tried

setTimeout( function() { loop(index+1) }, 100 );

i've also prefixed this. to loop in both cases - and no workee.

i think it's some sort of scope problem.
Indeed. What you're missing here is more basic than recursion - it's
a fundamental lack of understanding of the nature of JavaScript
scope. Unlike C++ or Java, the "this" keyword is not scoped, by
default, to the current object's context - it must always be
explicitly written out, and doesn't always refer to the object you
might think it does (from a classical OOP background). See
http://developer.mozilla.org/en/docs...:this_Operator

Another problem here is passing a string as an argument to
setTimeout. While it may technically work, it's terribly inefficient
- by using it, you're invoking the JS compiler to run your string as
code. Pass in a function instead, as you do in your second example.

-David

Aug 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
10013
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a class and a function in Python ??? Consider the following code fragments : # Fragment 1 begins a = 1
10
2562
by: Simon Elliott | last post by:
If I have two classes: class bar { public: void RunTheBar(void); }; class foo {
4
1723
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final question remaining: How to properly, simple and eleganty implement functions. The functions are encapsulated in the objects, the objects themselves are...
8
5351
by: Brett Robichaud | last post by:
I understand how code-behind can handle events for a page, but can I call a code-behind method from within a <script> tag in my ASP.Net page, or can I only call methods defined in other <script> sections? I can't seem to figure out the syntax for for calling code-behind directly. The method is within the class my page inherits from and is...
2
3537
by: mikepolitowski | last post by:
Hi folks, I am have been trying to solve this problem for quite some time now and would appreciate any advice. I have been trying to call a code-behind function that is defined in my aspx.cs from within a DataList <ItemTemplate> block using the <%# %> syntax. I would not have written here if I had not spent over 6 hours trying to find a...
26
2126
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
3
4694
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting...
16
517
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved...
21
4673
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of...
0
7693
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
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...
0
7917
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. ...
0
7962
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...
1
5501
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...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.