Objects in JavaScript



index
Disabled back button Next Section
printable version

Section 1: Defined

JavaScript Objects

In JavaScript, an object is an unordered collection of key-value pairs.

The key of a property can be a string and the value of a property can be any valid JavaScript value e.g., a string, a number, an array, and even a function.

Objects are dynamic bundles of data.


JavaScript has no Classes

Section 2: Declaration

As an object literal

Object literal declaration.

Using the "new" keyword

New keyword declaration.

As a generic object

Generic object declaration.
Section 3: Attributes

Attributes of JS objects

Objects are a HashMap Attributes.

Attributes are called properties

Section 4: Object Methods

Properties can be functions

.

Properties can be set to named functions

.

Literal - Can create by just assigning an anonymous function to properties.

.

Functional - Can be set in functional created objects by setting the field to an anonymous function.

.

Functional Named - Can be set in functional created objects by setting the field to a named function within the object.

.
Section 5: This

This is a reserved keyword.


What is "this"?

.

What is "this"?

.
Section 6: Constructors

Use a functional construction to use constructors.

.

Constructors are just functions

.
Section 7: Functional Objects

Functions are objects

Functional Objects.
Functional Objects.

Functions have a prototype of Function:Prototype

Functional Objects.

Example

Functional Objects.

Functional Objects & Instantiation

Instantiation.
Section 8: Prototype Chains

When it comes to inheritance, JavaScript only has one construct: objects.

  • Each object has a private property that holds a link to another object called its prototype.
  • That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.
  • By definition, null has no prototype, and acts as the final link in this prototype chain.

The above was taken from Inheritance and the prototype chain.

When you create an object, that object automatically gets a prototype.

There is an excellent video explanation in JavaScript Classes vs Prototypes

Inheritance Structure.

Here are some additional references that are useful:


Creating Prototype Chains

The Object.create function can be used to create an Object with any other Object as it's prototype.

More on Setting an Object's prototype.

Creating with prototypes.
Prototype Chains.

Own Properties

A JavaScript object can have either own or inherited properties.

More details can be found in Understanding Own Properties of an Object in JavaScript

Own Properties.

Property shadowing

When creating a property on an object that has the same property name on its prototype chain it will shadow the property on its prototype.

Property shadowing.
Attribute finding.

Prototype Chain (Watch Window)

Prototype Chain (Watch Window).

Prototype Chain

Prototype Chain.

Prototype Chain

Prototype Chain.
Section 9: Arrays or Objects?

Arrays are objects (not an instance of "Object")

Arrays.

See Javascript: Understanding Objects vs Arrays