Creating objects in JavaScript

I've been messing around with JavaScript for years now, but have been more of a JQuery hacker. I thought it was time to learn the language properly as its becoming increasingly common in web applications these days. This is a bit of a dummies guide, but I guess you need to start somewhere...

JavaScript is a dynamically typed language where objects can be throught of as groups of properties. A property can one of six basic types: numbers, strings, Booleans, objects, functions, and undefined values.


When we create an object using a constructor function in JavaScript the new keyword is syntactic sugar for Object.create. The function Monkey is just a normal function. If we don't use the keyword new, then this refers to the current global context which, for a webpage, would be the window object. When we do use new a new object is created and the context of this is set to the new object. The capital letter of the Monkey function is of course just a style preference.

Here is the same example, but using the Object.create method. notice that we have to set alot of meta data about each of the properties.


In ECMA 6 there is a new class keyword for creating objects. Its actually just syntatic sugar for creating exactly the same as above:

Popular posts from this blog

A Simple 3 Layer Architecture

Parsing Visual Studio Files with Powershell