JS: Object Overview

By Xah Lee. Date: . Last updated: .

This page is a overview of JavaScript object system. It's called prototype-based object system.

If you don't know how to create object, read properties, first see: JS: Object Basics.

Summary of JavaScript's Object

  1. Object is a collection of key/value pairs. Each key/value pair is called “property”.
  2. Most objects have special purpose, and internal data. For exampe, Function object is a function. Array object has a magical length property. RegExp object is used to match string pattern. Date object holds date/time data, etc. The one object that doesn't have special purpose is the data object, e.g. {key1:1, key2:2}.
  3. Each object has zero or one parent object, called its “prototype”. When a property is accessed, JavaScript will go up the parent object or parent of parent etc to look for the property. (this is called “inheritance”.)
  4. Each object has a special info attached called the [[isExtensible]] internal slot. It specifies whether new properties can be added to the object
  5. It's useful to distinguish objects by their origin: {standard object, hosted object, user-defined object}.

Following are details.

What's JavaScript Object

A JavaScript object is a set of unordered key/value pairs.

{Arrays, functions, date, regex, …}, are specialized objects. They have special properties and behaviors than normal objects. But they still are key/value pairs.

[see JS: Object Type]

Standard Object, Hosted Object, User-Created Object

It is useful to distinguish objects by where they came from. We have the following sources:

  1. Standard Object → Objects from the JavaScript language. For example, arrays, functions, dates, regex, Math, etc.
  2. Hosted Object → Objects from the hosting environment. For example, in DOM, there's “document” object as in document.getElementById(…). In Browser, there's “window” object [see JS: Browser's Window Object]. In node.js, there's “http” object as in (require('http')).createServer(…).
  3. User-defined Object → User defined. For example, let obj = {…}.

Object Internal Slots: [[Prototype]], [[isExtensible]]

Each object has many associated info called “internal slots” by the spec. The two important ones you need to understand for coding JavaScript are:

Note: In JavaScript spec, “Internal Slots” are indicated by double square brackets [[]].

Note: “Internal Slots” are implementation detail. They are not object properties. JavaScript programers don't need to know about interenal slots, but is very useful to explain some JavaScript behavior.

ECMAScript 2015 §Ordinary and Exotic Objects Behaviours#sec-ordinary-object-internal-methods-and-internal-slots

Ways to Create Object

There are 3 major ways to create data object:

  1. Literal expression: {…} → Most convenient and most used.
  2. Object.create() → Most powerful and flexible way to create object, because you can specify parent object, properties, property attributes, all in one function call.
  3. Keyword new, in new function_name() → complex behavior. It tries to be similar to Java's object oriented programing concepts, but the result is very complex. For some standard objects, such as Date, it's the only way to create it.

[see JS: Object Literal Expression]

[see JS: Object.create]

[see JS: Operator “new”]

Note: remember that function and array are also objects. So, when you define a function (For example, function f(…){…}) or create a array (For example, ar = [7,2,4]), you are also creating objects. [see JS: Understand JS Array]

JavaScript Object Property Overview

JS: Property Overview

Object and Inheritance Topic

  1. JS: Object Overview
  2. JS: Object Type
  3. JS: Determine Type of Object
  4. JS: Object Literal Expression
  5. JS: Prototype and Inheritance
  6. JS: Create Object
  7. JS: Create Object with Parent X
  8. JS: Get/Set Prototype
  9. JS: Prevent Adding Property
  10. JS: Clone, Deep Copy Object/Array
  11. JS: Test Equality of Objects
  12. JS: Object Object
  13. JS: Object.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.