Reputation: 29
How does the memory work in JavaScript? Is there a stack? A heap? How does memory management work?
When does the binding between the varriable and its place in memory occurs? Before or during runtime?
Is there modules or anything similar in JavaScript?
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.
Upvotes: 2
Views: 209
Reputation: 707396
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.
Upvotes: 3
Reputation: 6752
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.
Upvotes: 1