Skip to content

Javascript Introduction

onteria edited this page Jun 6, 2011 · 1 revision

JavaScript In General

JavaScript is a dynamically typed language where functions are first class objects, and the concept of prototype based inheritance is highly prevalent.

Dynamic Typing

In terms of dynamic typing, a variable can be reassigned different types of values without needing to use casts:

> var myvar = 3
> myvar
3
> myvar = "Hello World"
'Hello World'
> myvar
'Hello World'
> myvar = { x: "y" }
{ x: 'y' }
> myvar
{ x: 'y' }
> 

Here we have a variable, myvar, which is assigned different types of data freely. It is important to note though that this does not mean that types are non-existent:

> var myvar = "Hello World"
> typeof myvar
'string'
> var intvar = 3
> var stringvar = "3"
> intvar == stringvar
true
> intvar === stringvar
false

So myvar holds a string type with the value of "Hello World". In the next example, a variable holding an integer, intvar, and a variable holding a string, stringvar are created. If we do a simple comparison, the values turn out to be equal. Indeed their contents are both 3. However, if we also compare their types, the comparison fails as one is a string and the other is an integer. This concept is often a source of confusion for newcomers, so it's very important to keep these differences in mind.

Functions As First-Class Objects

This concept will be introduced in more detail later, but it's important to introduce the basic concept. In JavaScript, functions become objects that can be passed around and contained within data structures. You can even assign a function to a variable:

> var myfunc = function() { console.log("Hello World"); };
> myfunc();
Hello World
> var myotherfunc = function(function_to_call) { function_to_call(); }
> myotherfunc(myfunc);
Hello World

Here a function is assigned to a variable, myfunc. This variable can be then used to call the function. The another function, myotherfunc is created which takes a function as an argument, then calls it. By passing in the previously created myfunc, this function it holds is called and the same output is produced. The ability to use functions in this manner becomes very powerful.

Prototype Inheritance

The concept of Prototype Inheritance will also be discussed in a later section. As an introduction, prototype inheritance allows an object to be created with the functionality of another object as its base.

> var myobj = {x: 1, y: 2};
> var myotherobj = Object.create(myobj);
> console.log(myotherobj);
{}
> console.log(myotherobj.__proto__);
{ x: 1, y: 2 }

Here, a simple object is created with property x set to 1, and property y set to 2. Then a second object is created, myotherobj, based off of myobj. Now, every object has a property called a prototype. Methods and properties can be assigned to myotherobj without affecting myobj that it is based off of. However, if a method or property is accessed that doesn't exist in myotherobj, it will be searched for in myobj.

> myotherobj.z = 3
3
> console.log(myotherobj.z);
3
> console.log(myotherobj.x);
1

This inheritance can also be chained, providing the ability to create custom objects from other objects.

The JavaScript Backend

The language used by node.js is JavaScript. On the backend it uses the Google V8 JavaScript engine. This is the same engine used in the Google Chrome browser. V8 implements the Ecma 262 standard 3rd edition (also referred to as ES3), as well as some of the functionality in the 5th edition (also referred to as ES5). ECMAScript 5 and WebKit/JavaScriptCore has information on the parts of ECMAScript 5 that have and have not been implemented.

Not Just Your Browser JS

There are many developers who come to node.js from a browser based JavaScript background. However there are a few points to keep in mind about working with JavaScript in node.js. First off is there is no document object as in a browser. Node runs applications, so it doesn't have a concept of a browser document. Instead it works with a process.

> console.log(process);
(node) Use process.env instead of process.ENV
{ title: 'node',
  version: 'v0.4.8',
  installPrefix: '/usr/local',
  versions: { node: '0.4.8', v8: '3.1.8.16', ares: '1.7.4', ev: '4.4', openssl: '0.9.8l' },
> console.log(document);
ReferenceError: document is not defined

Another difference is that cross browser compatibility as an issue turns into potentially cross platform compatibility. For example, on Mac OSX the user's home directory is located in /Users/username. However on Linux it is located at /home/username. To list all the potential platform compatibility issues would turn into another guide in itself. However it is worth noting that node.js helps to screen you from some of these issues when, for example, working with networking functionality.

Clone this wiki locally