With JavaScript supported in every browser, it is an incredibly easy language to experiment with. You don't need a compiler or an interpreter or an IDE, because you already have one. Here, try this in the browser of your choice:
- In Chrome press Ctrl+Shift+J
- In Firefox press Ctrl+Shift+K
- In Internet Explorer press F12
- In Safari press Ctrl+Alt+I
- In Opera press Ctrl+Shift+I
console.log("Hello world!");What happened? You just executed JavaScript code in your web browser. Pretty neat, right? Okay, how about something a little more useful. Say you want to figure out how much money you're down today if you invested in 10 shares of Apple (AAPL) a year ago. You would type in
(571.7 - 408.38)*10;And find out that you're down $1633.20. Not such great news, but at least you can use the console as a calculator. You can do anything in the console that you can do with a normal JavaScript file, but there's one caveat. Entire code blocks have to go on one input line, so declaring functions or large objects is a bit problematic. Firefox has a slick scratchpad to get around this issue. Press Shift+F4 to bring it up, and then you can enter as much JavaScript as you like, nicely formatted, and press Ctrl+R to run it. The output will appear in the console.
That capability is pretty incredible, if you think about it. Every computer comes ready for web development out of the box with no downloads necessary. If you're using Windows, you'd have to download a compiler or interpreter for almost any other language, but not JavaScript. Now that you can write and run your own JavaScript, you just need a good book to learn it. Here are a couple contenders:
VS. |
JavaScript: The Definitive Guide
Weighing in at 1100 pages, JavaScript: The Definitive Guide is not to be taken lightly. If it is anything, it is aptly named, as it contains everything you could possibly want to know about JavaScript. It's not quite as long as it seems because the last 400 pages are language reference material, but 700 pages is still quite a lot to read through. Yet those pages do not disappoint. They are full of detailed descriptions and examples covering every aspect of the language in a satisfyingly clear, no-nonsense way.
I've found that there are three types of technical books that appeal to me: those that tell great stories or analogies, those that make great use of visuals, and those that present clear and relevant examples. This book doesn't have much in the way of stories or pictures, but it makes up for that with examples in spades. Each example is well thought out, easy to understand, and focused on the topic at hand. Beyond that, they are actually nontrivial and are potentially useful in your own programs.
The examples are really the gravy, though. The meat of the book is the explanations of the language features. Every page is filled with deep, sharp analysis of how to use the language, how the features are implemented, and how to get the best performance out of it.
I started out thinking that I would skim through most of the book, figuring that I could just pick up the syntax and skip a lot of the prose because it would be superfluous. But I found that I was reading almost everything, and spending a decent amount of time on it. Amazingly, despite its length, there was no wasted space, and it never got tiring. On the contrary, the whole book was pleasantly engaging, and I would definitely recommend it for JavaScript programmers of all levels. If what you want to do is possible in JavaScript, then it will be in this book.
JavaScript & jQuery: The Missing Manual
Compared to JavaScript: The Definitive Guide, this book was a letdown. Only about 20% of it actually dealt with the JavaScript language, and it was a pretty cursory treatment at that. There was no explanation of some of the most important concepts in the language, like closures and classes. Even objects were barely mentioned, when one of the most important things to come from JavaScript is JSON (JavaScript Object Notation) and its use in making dynamic web applications with Ajax.
The other 80% of the book was about jQuery, and David McFarland did a more than adequate job of covering that. The chapters were full of examples that were presented in a tutorial format so that you could type the code into a text editor, see the working examples, and experiment on your own. I liked that aspect of the book, so as a manual for jQuery, it hit the mark.
For a beginning web programmer who wants to learn just enough JavaScript to use jQuery and make dynamic, interactive web applications, this book would probably be perfect. That was not what I was looking for, though. I find that learning frameworks like jQuery is best done within the context of a project, and online resources are much more useable because they are searchable and have less risk of being out-of-date.
What I wanted was a book that went into plenty of detail and analysis with the JavaScript language and pointed me in the right direction with the jQuery framework. This book certainly fell short of those expectations. After trying out two books from The Missing Manual series with unfulfilled high hopes, I think I'm ready to pass on any more of these books. If you're looking for a book that will just tell you what you need to know to get a job done, this series will probably work for you. But if you're like me, and you want to get deep into the language, they're not going to be enough.
JavaScript: The Language
After spending a decade and a half with static compiled languages, JavaScript is my first dynamic interpreted language, and the difference is striking. I'd like to spend a little time here exploring the differences between JavaScript and C++ to show how pleasant JavaScript is to program in by comparison. Let's start with variable declarations. In C++ you have to think carefully about what type every variable is that you declare. Now in certain domains that's important because the variable types will have a big impact on performance, but let's assume that we're programming in a domain where that won't matter. You still have to declare your variable types in C++:
int samples[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};You should always initialize your variables, so I did that. Now look at what's going on there. It's a little redundant to declare the variable type and then assign it to a value that makes it obvious what the variable type should be. JavaScript is much easier:
int sum = 0;
double mean = 0.0;
double stdev = 0.0;
var samples = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];Nice! By the way, in most cases in JavaScript there are no integers, only reals, so it doesn't matter if you use '0' or '0.0'. You're going to get a real-valued variable. Declaring variables like this will spoil you. I've caught myself trying to do it in C++ now out of habit, so you've been warned.
var sum = 0;
var mean = 0;
var stdev = 0;
Alright, let's say you want to create a quick object to hold some data on your pets. In C++ you'd have to at least create a struct and instantiate it. That would look something like this:
struct Pet {And then there's JavaScript:
string name;
string type;
string breed;
int age;
double weight;
}
Pet myPet = {"Twitch", "cat", "Siamese", 10, 8.4};
var myPet = {We can just create the object on the fly without having to declare an object type as a struct or a class. But now let's say we wanted to make the pet more permanent as a class. In C++ you might do this:
name: "Twitch",
type: "cat",
breed: "Siamese",
age: 10,
weight: 8.4
};
class Pet {You might want to include setter and getter methods for any of the members (class variables) that you'd want to change later, but I'll omit those for brevity's sake. I'm also not including any error checking code that would be required, like making sure the age and weight are within reasonable bounds. Now here's the equivalent JavaScript:
private:
string _name;
string _type;
string _breed;
int _age;
double _weight;
public:
Pet(string name, string type, string breed,
int age, double weight) {
_name = name;
_type = type;
_breed = breed;
_age = age;
_weight = weight;
}
}
Pet myPet = new Pet("Twitch", "cat", "Siamese", 10, 8.4);
function Pet(name, type, breed, age, weight) {Once again JavaScript is more compact and far less redundant. What a pleasant change of pace. It's not readily apparent that 'Pet' is actually a class, but once you get used to JavaScript, it becomes easily recognizable as one. I was amazed at how quickly I picked up most of the language, and it is, dare I say, quite fun to program in it.
this.name = name;
this.type = type;
this.breed = breed;
this.age = age;
this.weight = weight;
}
var myPet = new Pet("Twitch", "cat", "Siamese", 10, 8.4);
The comparisons I went through here only scratch the surface of JavaScript's efficiency. Things that took lines and lines of tedious code in C++ or Java can be done with much less code in JavaScript. It's much easier to put your thoughts directly into code. You don't have to muck around with variable types and tedious class declarations. It feels like you can express the problem more directly in JavaScript without all of the cruft. If you haven't learned a dynamic language yet, I highly recommend this one. It will expand your mind and make you a better programmer.
Back to the Books
So which book is the one for you? If you want to know all about the JavaScript language and just enough about jQuery to get started programming dynamic web pages, go with JavaScript: The Definitive Guide. I would especially recommend getting it in an electronic version for two reasons: you don't want to break your back lugging the dead tree(s) version around, and as a reference it is infinitely more searchable on your computer or Kindle. Of course, if you do get the print version, you can also use it as a combination foot stool and bug/rodent smasher.
If you want to start programming dynamic web pages right away and don't care much about advanced features of JavaScript, go with JavaScript & jQuery: The Missing Manual. If you learn better from tutorial style examples, this book will be even better for you. You should be writing flashy interactive web pages in days.
As for JavaScript itself, enjoy it. If it's your first programming language, I envy the experience you're about to have. I can't imagine what it would be like learning JavaScript as a first language, but I'm sure it would be much easier than C++. Don't shy away from C, though. Once you know JavaScript, learning C will show you a whole other realm of programming that will serve you well. If you're coming to JavaScript from a static language, enjoy the freedom that JavaScript gives you. You'll probably wonder why you waited so long to learn a dynamic language. I know I do.
No comments:
Post a Comment