How javascript objects work

The longer that I use javascript the more I realise it’s a great language. It’s a language that’s easy enough for people with basically no coding experience (for example: me ten years ago) to hack something together while the deeper you go into it the more great language features you find. For me it’s been a language I could grow into. This year I scoff at how much more I know than I did a year ago. Last year I remember thinking exactly the same thing.

This is a series of articles about how to use the javascript language features to write object oriented type code. It’s designed for people who are already sold on the idea of bundling functionality up into objects and want to know the javascript way of doing things. The articles are designed to take you through all the language features that make object oriented code possible. It should be useful if you’re writing bare naked javascript or if you’re using some sort of javascript framework.

Building simple objects – javascript objects are just a collection of key/item property pairs. Learn how javascript objects work and the different syntaxes for creating them.

Functions are first class objects – javascript functions are just a special type of object which means you can do anything with them that you could do with any other type of object. This is important to understand when creating objects in javascript because object methods are just properties that happen to be functions.

Anonymous functions – anonymous functions are one of the really powerful dynamic features in javascript. Learn about the differences between the function statement and the function expression and how anonymous functions can help make your javascript code clearer and more concise.

Constructor functions – constructors are functions that can automate object construction. They are javascript’s object factories! Learn about using constructors to make objects of the same type and how the new operator works.

Closures – a really powerful dynamic language feature of javascript. They make it easy to pass state around your application without having to save global variables.

Object prototype – Although constructor functions look a lot like classes, javascript does not have a class based object system like C# or Java. Instead it has a prototype based object system.

Prototype chaining – this is used to build new types of objects based on existing ones. It has a very similar job to inheritance in a class based language.

Javascript method context – in languages like C# and Java you never really need to give a thought to the this operator. In javascript things are a little more complicated due to functions being first class objects.

User group talk

I have written this up as a user group talk with samples and slides. Please feel free to use the slides to give the talk yourself or use it as a starting point for your own talk. Licensed under creative commons.

Comments

  1. alan Says:

    Hi
    I have really enjoyed these tutorials being a newcomer to OOP, you explain things very clearly!!
    However I am struggling to see how to use this new OOP knowledge in a real world system??
    Is it possible to acess files and databases as part of the processing for editing say, and is it possible to save data to files and databases??
    Probably a dumb question but I would appreciate your response
    Kind regards
    Alan

Leave a Reply