Features of JavaScript that C developers fail to take advantage of? [closed] Features of JavaScript that C developers fail to take advantage of? [closed] javascript javascript

Features of JavaScript that C developers fail to take advantage of? [closed]


There is not nearly enough room here, besides, since there is at least one great source I just post a link:

To learn the REAL (deep conceptional) basics of Javascript watch the videos from Douglas Crockford at http://developer.yahoo.com/yui/theater/

He's one of the most important people in bringing to light the (until then) hidden great power of the language. Besides, he's a very good presenter even for such a relatively dry topic as a programming language.

However, those videos are not very suitable for beginners in programming, but then the question you raise is not a useful one for them either since any answer requires a deeper understanding and quite a bit of practice as well.

Core Javascript is very RAW. You do things that in other languages happens at compile time, for example, when your JS program is loaded you already execute code which assembles objects, "classes" etc. dynamically - and after that initial loading and execution it can be a COMPLETELY different piece of software. You don't have these two stages in any other (old) language (you can do it in Ruby, similar story, those modern languages share certain features). It is therefore NOT helpful to compare JS and C/C++, especially not when you only look at syntax leven stuff, in which case you'll learn nothing at all. It can provide the illusion of learning, though. WATCH CROCKFORD.


A block scope example for the discussion in another answer here, since I've no other place to put it now (for convenience wrapped in function "foo"). There is a claim you cannot have block scope in Javascript. Granted, in other languages you use a much more familiar syntax, often just enclosing {} or sometimes BEGIN... END. Anyway, the below delivers EXACTLY block scope. Yes, it uses the "function" keyword, but just because you're not used to it does not change the facts.

function foo () {    var a = 1;    (function () {        //THIS IS BLOCK SCOPE        var a = 2;        console.log(a);    })();  //IMPORTANT: the "()" executes it immediately, INLINE    console.log(a);};

Calling foo():

21

If somebody still says "block scope is impossible (in JS)" they should point out what "block scope" is supposed to have that the above example does not provide, apart from needing some getting used to and being less esthetically pleasing than wrapping code in {} or BEGIN...END.


I'd say

  1. Closures
  2. Nameless inline objects
  3. Dynamic typing
  4. Garbage collection
  5. Dynamic arrays and objects

but however someone coming from C may not actually "miss" these features... one could happily use javascript as C (of course losing a lot doing so and also creating a true nightmare for fellows programmer that actually know javascript if they have to work on the same project).


(Removed previous example)

I think the #1 cause of confusion is lack of block scope in JavaScript:

Here's an example:

if (true) {    var foo = 7;}alert(foo);

"7" is alerted. Not an expected result to a C++ programmer.