2
  1. How does the memory work in JavaScript? Is there a stack? A heap? How does memory management work?

  2. When does the binding between the varriable and its place in memory occurs? Before or during runtime?

  3. Is there modules or anything similar in JavaScript?

  4. Also, would you say JS is portable? And reliable? Please give a short explanation to your answer.

I was searching for answers on the internet, but I don't seem to find any. Quick answers are appreciated as well.

oranitel
  • 29
  • 2
  • I believe not even the **C** standard says anything about a stack or a heap - that should be just an implementation detail, albeit an *incredibly* common one. Neither is B the concern of the language specification. –  Jul 09 '11 at 22:13
  • I'd like to point you to [V8 docs](http://code.google.com/apis/v8/design.html). Also keep in mind V8 is just one of the various JavaScript engines out there. – jweyrich Jul 09 '11 at 22:27
  • @delnan: But it's still a valid question. Maybe he's just interested. – jforberg Jul 09 '11 at 22:35
  • @jforberg yes, i have a paper due for tommorow... – oranitel Jul 09 '11 at 22:59
  • 1
    @oranitel: We can solve many problems, but we can't fix your bad habits :) If you're still confused, try skimming through the Wikipedia article on garbage collectors: http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) It's not very good, even for WP but it'll bring you up to speed about JS's memory management. – jforberg Jul 09 '11 at 23:04

2 Answers2

3

Memory is managed for you in javascript so you don't really have to worry about it other than making sure you don't use ridiculous amounts of it. When there are no references left to an object or it has gone out of scope, it will be freed by a garbage collector. How it works under the covers is really implementation dependent and not defined by the language.

Even function frames (e.g. local variables) work this way (and not the traditional stack oriented way) which allows for javascript "closures" which are function frames that are not released until no embedded functions have references to them any more.

Javascript code itself is completely machine independent so it's very portable. In practice, the portability of an application typically depends upon the libraries that javascript interacts with (e.g. browser DOM) more so than the language. It's documented by a series of ECMA specifications and there are different version numbers of that specification which define various new features as the language has evolved.

I consider javascript very reliable and, as long as you don't try to use recently introduced features that aren't available in different implementations, there are rarely true javascript issues. There are tons of cross-browser compatibility issues, but those are nearly all NOT in the language itself, but in the browser DOM or the interaction between the language and the DOM.

I'm not sure what you mean by "moduls".

Javascript is an interpreted language so there is no fixed binding between a variable and it's place in memory. All variables are referenced by their name and it's up to the implementation to determine how to best resolve the connection between a name and a specific piece of memory that stores the value.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • "moduls" -> "modules", at least as far as i understood it – cHao Jul 09 '11 at 22:16
  • I interpreted it as "modulus". Not sure what was intended. – jforberg Jul 09 '11 at 22:23
  • "emory is managed for you in javascript so you don't really have to worry about it. When there are no references left to an object or it has gone out of scope, it will be freed."- im not asking questions because im trying to program. i have apaper to submit tomorrow about a programming language and i chose JS. this is one of the questions i've been asked nd was unable to find a clear answer. – oranitel Jul 09 '11 at 22:39
  • 1
    Objects are allocated in memory and are reference counted (with how many in-scope pieces of code have a reference to the variable/object). As long as the reference count remains non-zero, the object is preserved. When the count goes to zero, it is eligible to be freed by the garbage collector. Garbage collectors have all sorts of special problems to solve such as two objects that each hold a reference to each other, but nobody else holds a reference to either of them (reference counts are non-zero, but it is actually OK to free them). – jfriend00 Jul 09 '11 at 22:58
1

A. There is only the heap. Javascript uses automatic garbage-collection. May I assume you have experience with C / C++? In JS, the recipe for success is pretty much: "Just forget all about memory management and you'll be fine".

B. The binding occurs at runtime. Remeber that Javascript is not a compiled language, so there is no compile-time, only runtime.

C. Like all c-style languages, Javascript uses % as a modulus operator, as a quick google search would have revealed.

D. It's very portable, since it runs on the browser, not directly on the system. Pretty much any system that runs Firefox or Chrome will run Javascript, meaning MS Windows, Linux, Mac, the BSDs, any modern system really.

D'. There are several implementations of Javascript. Asking "Is Javascript reliable?" is like asking: "Are cars reliable?". There are many different cars, which are more or less reliable; same for Javascript.

jforberg
  • 6,537
  • 3
  • 29
  • 47
  • 1
    JS need not run within a browser. It's currently the most common environment, but the language is independent of browsers (evidence: node.js). And the question sounds like one of those "other" environments may be what's intended. – cHao Jul 09 '11 at 22:20
  • @cHao: True, but 99% of the time, it runs on a browser. Perhaps you're right and he is wondering about those "other environments". – jforberg Jul 09 '11 at 22:25
  • i didnt mean modulu. i meant modules. – oranitel Jul 09 '11 at 22:37
  • sone...i didnt see i wrote that mistake – oranitel Jul 09 '11 at 23:24