Hello, all.
By request, I’ve got planned out a small series on Javascript (or, as it’s called now, ECMAscript). As a bit of background, Javascript is a web scripting language used primarily as client-side code. As such, it’s interpreted by the browser instead of being compiled, which makes it easier to build and test a script. Modern browsers are starting to compile Javascript as it’s being run on the page, though, which allows the code to run a fair bit faster (as it’s native code, instead of having to be interpreted).
Javascript allows you to do a lot of dynamic things to a webpage, which I will be demonstrating in later how-to’s. For now, I thought I better start with the basics. Javascript is a C-style language, with Objects making up a majority of what you use. You don’t need to create your own classes like you do in Java, though. In fact, creating classes is nowhere near as simple as in other Object Orientated languages. I will leave that lesson for another tutorial, though.
There are a number of predefined classes in javascript, which include:
- Object;
- Function;
- String;
- Number;
- Boolean;
- Array;
- Date; and
- RegExp (regular expression).
Object is obviously the root of all objects within the language. Every other class extends from Object.
Function is the type given to all variables defined as a function (which I will demonstrate later on). It allows you to create callable functions, as well as class prototypes.
String is obviously for strings of characters within the language. You can define a string using double (“) or single (‘) quotes for string literals.
Number encaspulates both integer and double precision types available in most other languages. These can be defined as normal number literals (using decimal or hexadecimal values).
Boolean destinguishes truth values, just like in most languages. You define it with the literals true or false. You can also use other objects as booleans, which I will cover in a later tutorial.
Array is self-explainitory. Arrays can contain mixed datasets, as well as grow and shrink as you use it.
Date is a complex class. It contains information about the current date and time of the system the code is running on.
RegExp is the regular expression class. Most of the time you won’t need to use this class, unless you are creating code in which it’s necessary.
There’s also another object called Math, which contains mathematical operations. Math acts like a static class in Java (in which all methods in the class are static).
Now I guess you’re wondering, how do you define these objects within actual code? It’s actually quite simple, using the var keyword:
var obj1 = new Object(); var obj2 = {}; var func1 = new Function(); var func2 = function(){}; function func3(){} var str1 = new String(); var str2 = "test"; var str3 = 'test'; var num1 = new Number(); var num2 = 123; var bool1 = new Boolean(); var bool2 = true || false; var arr1 = new Array(); var arr2 = []; var date1 = new Date(); var date2 = new Date(milliseconds); var date3 = new Date(timeString); var date4 = new Date(year, month, day, hours, minutes, seconds, milliseconds); var re1 = new RegExp(pattern, modifiers); var re2 = /pattern/modifiers |
You can also define variables as global by simply not using the var keyword. The third version of defining a function above is simplest way, and is what you’d usually use when creating a simple script. This is the way I will be using for the next tutorial, to give you an idea of how functions work. Also, you may notice the two ways of creating Objects and Arrays. The second way for both of them is called JSON (JavaScript Object Notation). I will cover JSON in a later tutorial.
Javascript also contains the usual C-style if/else if/else and switch statements, as well as while and for loops. There is also a for each loop, which iterates over public variables within an Object. Plus, exceptions are possible within Javascript using throw in try/catch blocks.
I think that’s about it for the introduction to Javascript. Stay tuned for more tutorials coming soon.
Robert