How javascript objects work

Javascript is a flexible language that supports structuring code in procedural, functional or object oriented ways. Although it is not exclusively object oriented like C# or Java, there are many language features that support creating and using objects if that is the way you like to structure your code.

This series of articles digs deep into each of those language features and shows how they can be used to create object oriented code. Learn how to create and manage objects in plain javascript or just understand what your favourite framework is really doing.

I hope it helps you appreciate the beauty and flexibility of a such an important language, which was designed to make it easy for novice programmers to be productive while having more advanced features for experienced developers building large and complex systems.

Creating objects in javascript

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.

Posted on 17 Aug 08 by Helen Emerson (last updated on 14 Apr 13).
Filed under Javascript