Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Fellow Stackers,

In the first Computer Science class I took, the professor used C++ as a teaching language. He emphasized that the important things to understand are the concepts of programming, not just the language itself.

In the past few years, I've fallen in to a career in database driven web development. At first, I was busy learning to use the server-side programming tools, namely ASP.net. Most of my fellow co-workers from my early days didn't use much client-side scripting code, but I've been making a big effort to learn to write good client side scripting.

I've begun using jQuery in earnest.

The first thing that most any C++ or .net developer will notice about JavaScript upon first encountering it is that the syntax is remarkably similar to C-style languages.

There are important differences. JavaScript uses variant data types, allows for arrays to be declared in-line, and uses it's own object syntax with curly braces. Above all, JavaScript is an interpreted language that allows for a user to interact with a "document-object-model" using a web browser which C++ is typically compiled into native code (or sometimes managed code). It would be silly to make more than a superficial comparison of the two languages.

What I would like to ask is, "As I learn to write JavaScript code, part of my mind can't help but think of it as being like C code because it looks like C code. What mistakes am I likely making on account of this? What useful features of the language am I likely not using?"

Update: I edited my question title because it's clear that my old title was ambiguous :-/

like image 533
Vivian River Avatar asked May 11 '11 19:05

Vivian River


1 Answers

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():

2
1

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.

like image 94
Mörre Avatar answered Oct 16 '22 10:10

Mörre