Connecting Tech Pros Worldwide Help | Site Map

question about recursion and OO javascript

  #1  
Old December 11th, 2007, 02:45 AM
lielar
Guest
 
Posts: n/a
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

  #2  
Old December 11th, 2007, 03:55 AM
RobG
Guest
 
Posts: n/a

re: question about recursion and OO javascript


On Dec 11, 12:39 pm, lielar <lie...@gmail.comwrote:
Quote:
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...--------------^^--------------^
Quote:
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"?
Quote:
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.

Quote:
How do you solve the problem of 'this' in this
scenario?
You haven't identified a problem with 'this'.
Quote:
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
  #3  
Old December 12th, 2007, 08:35 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

re: question about recursion and OO javascript


lielar wrote:
Quote:
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>
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call a function syntax chirs answers 15 July 20th, 2005 01:05 PM