473,387 Members | 1,578 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,387 software developers and data experts.

question about recursion and OO javascript

Hi

I'm writing a javascript class object that creates objects of itself.
Take for example

function One(x){
this.x = x;
}

One.prototype.addOne = function(x) {
this.extra=x;
}

One.prototype.getOne = function() {
return this.extra;
}

One.prototype.print() = function() = {
return this.x.toString();
}

Then I want to retrieve it where you can retrieve many levels of
'extra'. How do you make a loop to keep on looking in to a child
values of printValus? How do you solve the problem of 'this' in this
scenario? Wouldn't there be a problem with memory leak?

Cheers
Patrick

Dec 11 '07 #1
2 1441
On Dec 11, 12:39 pm, lielar <lie...@gmail.comwrote:
Hi

I'm writing a javascript class object that creates objects of itself.
Take for example

function One(x){
this.x = x;
}

One.prototype.addOne = function(x) {
this.extra=x;
}

One.prototype.getOne = function() {
return this.extra;
}

One.prototype.print() = function() = {
Oops...--------------^^--------------^
return this.x.toString();
}

Then I want to retrieve it where you can retrieve many levels of
'extra'.
I don't know what you mean by "levels of extra". Do you mean assign
an object to "x"?
How do you make a loop to keep on looking in to a child
values of printValus?
You use recursion: test the argument, if it's an object, use for..in
to loop over all the properties and for each one, call the function
again. Otherwise, return its toString() value. If you don't know
what type of object you are dealing with, you may want to check if
it's an Array, Function, RegExp, etc.

You probably also want to collect the results of recursion and return
a single, collated string value.

How do you solve the problem of 'this' in this
scenario?
You haven't identified a problem with 'this'.
Wouldn't there be a problem with memory leak?
There may be a problem with too much recursion, but that normally
occurs way beyond what is reasonable in most cases.
--
Rob
Dec 11 '07 #2
lielar wrote:
I'm writing a javascript class object that creates objects of itself.
There are no classes, much less class objects.

http://developer.mozilla.org/en/docs...of_Differences
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Dec 12 '07 #3

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

Similar topics

12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
3
by: csx | last post by:
Hi all, Ive got a problem with recursion in Javascript. For this tree: http://www.pcm.uklinux.net/structure.jpg If you input node 3 (i.e. C) which is represented as 'values' in the array, it...
1
by: Sylaris | last post by:
hi, i have a problem which recursion fits perfectly, however; after working with the base function (which has no errors and works correctly) the recursions return a "function not defined" error in...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.