PDA

View Full Version : how does Javascript allocates memory


denis
09-18-2008, 03:29 PM
1) function caddr(head)
{
var k;
}
Q. does the above function allocates a memory in stack for the variable k;

2) function car(op1)
{
return function add(op2){return op1 +op2;}
}
var add = car(4);
Q.How does the above function does the memory allocation.

I am new to javascript so.please enlighten my confusion. any help will be greatly appreciated.
denis

RysChwith
09-19-2008, 08:26 AM
Not entirely sure I understand the question, but I'll do my best.

Variables in JavaScript exist in memory only so long as their scopes continue to exist. In your first example, k would exist in memory until until the function caddr() concludes. Once you hit that closing bracket, the variable is erased.

Your second example I don't entirely understand. I can tell you that the variable car, since it is assigned outside of any function, has a global scope and will exist as long as the page does. The variable op1 will exist for the duration of car(). The variable op2 isn't actually defined anywhere, so will probably produce an error.

It looks like you're going for a closure in the second example, and this is a topic I've never understood well. Hopefully someone else (Horus? Coot?) can pick up the slack there.

A few articles:
Preserving Scope in JavaScript (http://neo.dzygn.com/archive/2004/05/preserving-scope-in-javascript) (primarily the first section)
Javascript Closures (http://jibbering.com/faq/faq_notes/closures.html)

Rys

Horus_Kol
09-19-2008, 09:04 AM
to be honest, I've never really worried about memory allocation in javascript and PHP - it just happens (unlike C++ and Pascal where you have to allocate memory before using the variables)...
there are times when memory allocation becomes a big issue in JavaScripts (especially with Google Maps API, unfortunately) - but I haven't seen a decent memory management model for it (other than trying to restrict new variable assignment).
other than that, I've not yet written a JavaScript which will leak enough memory to be an issue

this seems more of a scope question, and I'd say that Rys has pretty much nailed it - the variable 'add' will retain its assigned value until it is assigned a new one or the user navigates to new page or refreshes the current one.
variables specified within functions are only accessible within those functions, and their assigned values will only be retained as long as that function is 'active'

denis
09-19-2008, 09:05 PM
thanks guys, and yes ryschwith....i was talking about closure...

RysChwith
09-22-2008, 08:24 AM
Ah, cool. If you figure it out, please explain it to me. :)

Rys

Horus_Kol
09-22-2008, 08:35 AM
closures look interesting - not sure if I get it all yet...

I don't see how the OP is talking about closures from the examples given, though.

denis
09-23-2008, 12:07 AM
hey RysChwith and horus_kol s by the way i have been using javascript for about 2 weeks.. :) so my 2 pennie worth of knowledge aint worth it... but just a curiosity do you guys know how to do multithreading in java, i have some basic knowledge but i am running in some problem....
Denis

.

denis
09-23-2008, 12:09 AM
and by the way what part of closure dont you understand... :) maybe.....i have the answer
:0

Horus_Kol
09-23-2008, 12:21 AM
hey RysChwith and horus_kol s by the way i have been using javascript for about 2 weeks.. :) so my 2 pennie worth of knowledge aint worth it... but just a curiosity do you guys know how to do multithreading in java, i have some basic knowledge but i am running in some problem....
Denis

.
Java != javascript... you do know that, don't you...

as for multithreading in Java - no idea, I'm afraid



on the topic of closures - things are a bit clearer now... it seems like it comes down to a way of using JavaScript's scoping hierarchy to allow variables and functions to hold the same name in different parts of the script...

denis
09-23-2008, 12:44 AM
wooopsss....
sorry about that ("java".compareTo("javascript") != 0) :)

by the way, if you dont mind it would be really great if you could talk a little more about closure......
denis