JavaScript Objects

Some things about JavaScript objects.

Basic objects

There are some different ways you can create JavaScript objects, each having their own purposes and benefits.

Object constructors

If we want to create multiple instances of an object, we can use object constructors to define a template. By convention, constructors are named with a capital letter.

The new keyword

As you can see, the object constructor is just a normal function. However, the important part is the new keyword, used when creating a new object instance of the constructor, which then returns the instance. Without it, you’ll have a normal function, which returns nothing (actually, undefined), and sets the constructor properties on the window object (overwriting any window property of the same name).

If you haven’t included 'use strict' in your file, you won’t even get an error about this.

Properties

A property on an object is more than just a name and a value. You can see those by using the property descriptor method.

Writable

With writable you can set whether it’s possible to change the initial value or not. You can define this with the define property method.

If you use 'use strict'; in your file, you’ll get a TypeError: Cannot assign to read only property 'name' of object error on trying to change the name, otherwise silence.

Enumerable

With enumerable set to false you can choose to hide the property from the object, which can be useful if you fx don’t want to include it in a loop.

Configurable

With configurable you can set whether it’s possible to redefine the property configuration or delete the property.

Getters and Setters

The get and set attributes on an object lets you specify a return value of a property using a function, while also giving access to set the property - just like any other property.