Welcome to my blog (and some JavaScript fundamentals)...

13 August 2008

Permalink: http://douggreenall.co.uk/site/?p=10

Well hello and welcome to my blog. So I suppose I should probably introduce myself and give you some kind of idea of what I'm trying to achieve here - well I'm a software developer by day... actually no, that's not strictly true: I'm a software developer, full-stop. I work on JavaScript frontend development for my employer during the day, but I also spend most of my evenings and weekends playing around with all sorts of other stuff (graphics engines, physics engines, neural networks)... yeah, I'm basically a big nerd. I've been programming since I was a kid and (perhaps somewhat incredibly) it still excites me so I figured I'd try and write about some of the things that I do and see if it's of any interest to anybody. Now I'm not really big on posting opinions on the web because in all honesty, mine are about as relevant as everyone else's, so that's not really what this blog's about - rather, I'm going to concentrate on refactoring and posting a whole load of (mainly JavaScript) code and components I've written over the years and try to explain how they work. Now it's very likely that the occasional opinionated quip will surface (because let's be honest, I'm as much of an egomaniac as everybody else) but I'll try to keep them to a minimum and focus on more objective and constructive matters. Anyway, I'll end this first post with some fundamental JavaScript code that you'll see again and again if you become a regular visitor here:

Ok, so we have JavaScript functions:

function AFunction(arg1, arg2)
{
  var nonPersistentVar1 = arg1;
  var nonPersistentVar2 = arg2;

  return (nonPersistentVar1 + nonPersistentVar2);
}


Which we invoke like so:

AFunction(2, 3);

Now thanks to the loosely typed nature of JavaScript, the output of this function could be any number of things, depending on the context in which it is called:

alert(AFunction(2, 3)); // 5
alert(AFunction('2', '3')); // 23
alert(AFunction('a', 'b')); // ab
alert(AFunction(2, 'b')); // 2b


(and various others, but we'll get to those later on...)

Modern programming however needs more than just procedural functions: we need object modelling (classes). Rather charmingly, a JavaScript class is simply a function with persistent data and member functions (methods):

function AClass(arg1, arg2)
{
  this._persistentVar1 = arg1;
  this._persistentVar2 = arg2;

  this.MemberFunction = function()
  {
    return (this._persistentVar1 + this._persistentVar2);
  };
}


We can create an instance of this object as follows:

var aClassInstance = new AClass(2, 3);

If we query the contents of this object we'll get the following:

alert(aClassInstance._persistentVar1); // 2
alert(aClassInstance._persistentVar2); // 3
alert(aClassInstance.MemberFunction()); // 5


I'll write more about this in my next post (as it's getting rather late, what with work in the morning and all) as well as including some examples of the kind of things we can achieve with JavaScript and a little imagination (and head-scratching)...

Welcome to my blog (and some JavaScript fundamentals)...
Status: Ready...
Close Window Maximise Window Minimise Window Minimise Window
Contact