Marked Up Part 3: Introducing Javascript
One of the biggest differences between HTML 5 and its predecessors, is its Application Programming Interfaces (APIs). The HTML 5 APIs let you automate and extend common tasks, such as file management, geolocation, and media processing. In fact, to understand and get the most out of HTML 5, you can’t avoid JavaScript.
The Good, The Bad, And The Ugly
Until recently, most developers tried to avoid working with JavaScript. To many of its critics, JavaScript is a derivative patchwork of other programming languages. According to legend, it was created in only 15 days; and in the following 20 years, many of its original defects have not been fixed.
Ironically, non of its perceived faults has stopped it from becoming ubiquitous. This has led Douglas Crockford, a senior web architect at PayPal, to write the influential JavaScript the Good Parts. Crockford argues that after you remove the bad parts, you reveal a subset of JavaScript that is reliable, readable, and maintainable.
Up-close And Personal
In order to understand the language in greater depth, let’s look at some of its key features.
Dynamic Not Static
One of the main tasks of any programming language, including JavaScript is to label, store, update, and retrieve individual items of data. Due to the fact that the value of each labeled item changes, individual data items are referred to as variables.
Most programming languages statically type their variables. This means that words, dates, lists, integers, and decimal values must be stored as text or numbers. JavaScript uses dynamic typing, this means that data is stored the same way regardless of whether it is a number, text string, date, etc.. While in many ways, this simplifies programming in JavaScript, it also means that certain types of errors can only be detected when the code is run.
Same As It Ever Was
Since it was originally released in the early seventies, C has become one of the most important and popular high-level language. Like many modern high-level languages, JavaScript uses the same syntax and conventions as C.
This means that if you know how to write a conditional (if or case) statements, or loops (for or while) with C, you should have no problems with JavaScript.
JavaScript also lets you organize code into blocks called functions. While there are some small differences, between C and JavaScript functions, they both accept one or more input, and return a single output.
Objective Truth
Today, most languages support object orientated programming (OOP). Using a traditional OOP approach, programmers organize their code into objects. An object is an abstraction that models the characteristics of a physical object in software.
Each object is a combination of actions (methods) and data (properties). At the code level, developers write classes that describe the object’s methods and properties. In addition to its internal properties and methods, an object can inherit properties and methods from other objects. When the program is run, it uses the class template to create (instantiate) one or many instances of the object.
In JavaScript, the definition of object is broader and less clearly defined, so that:
- Individual functions, variables, and lists of variables are considered objects.
- An existing object can be used as a basis, or prototype, to create new objects.
- It is possible to dynamically assemble new objects from a group of smaller objects.
Proponents of this approach claim that it is more flexible than the OOP, class-based models. It’s detractors claim that it is chaotic and difficult to understand.
A Functional Language
JavaScript is clearly one of the many direct descendants of C. It also also includes many elements of the List Processing (Lisp)language. Lisp was created in the late 1950s for Artificial Intelligence research. It is widely regarded as one of the first functional languages. In a functional language, a mathematical function is evaluated and returns a data set.
Recently, many developers have discovered that functional languages are better suited for managing asynchronous between a web client and a remote server, and its abilities to process complex lists. Since most web development takes place in an asynchronous environment, and is used for processing long and complex lists, it seems a natural choice.
Back To HTML
In the space of a few years, JavaScript has risen from being a necessary evil to the dominant programming language. It is not only available in the browser, but a server side version of the language, called Node.js is becoming increasingly popular. In addition, it has become an essential ingredient in HTML 5. In my opinion, it is now hard to separate HTML 5 from JavaScript. In the next article in this series, we will start looking at HTML 5 and its JavaScript APIs.