What's Object in JavaScript?

, , …,

A object in JavaScript is any value that is not a {string, number (NaN, Infinity) , true, false, null, undefined}. For example, {{…}, array, function, date, regex, …} are all objects.

A object is a collection of key/value pairs. A key/value pair is called property of the object. (property name/value) Best way to undertand JavaScript object is to think of it as {Python's Dictionary, Perl's Hash Table, Ruby's Hash Table}, but with automatic inheritance feature. ECMAScript §8#sec-8.6

There are different kind of objects.

Find Value's Data Type

How to check if something is a object?

Use typeof value

// following are not objects

typeof "abc";                   // ⇒ string

typeof 3;                       // ⇒ number
typeof 3.5;                     // ⇒ number
typeof NaN;                     // ⇒ number
typeof Infinity;                // ⇒ number

typeof false;                   // ⇒ boolean

typeof undefined;               // ⇒ undefined

typeof null;                    // ⇒ object
// Historical baggage. 「null」 is really not a object. Should be “null”

null is a special case. Although typeof null returns "object", but it doesn't have the essential quality all JavaScript objects have, namely, it's not a key/value pair; you cannot add properties to it.

null.p = 4;  // TypeError: Cannot set property 'p' of null

What's the use of null?

The only possible use is that sometimes you might set a variable to null as default initial value. (unassigned variable has default value of undefined)

Following are example of objects.

// normal object
typeof {};                      // ⇒ object

// array is a special object
typeof [3,4];                   // ⇒ object

// regex is a special object
typeof /\d+/;                   // ⇒ object

// date is a special object
typeof (new Date());            // ⇒ object

// Math is a special object, hosting properties such as Math.PI, Math.sin ….
typeof Math;                  // ⇒ object

// function is a special object
typeof (function () {return 3;}); // ⇒ function

Object = Key/Value Pairs

The essence of JavaScript Object is that it's a set of key/value pairs, called properties. (similar to other languages as hash table, dictionary, associative array.)

You can add properties to function, date, regex. Even though they are not usually used as key/value pairs, but you can still do so, because they are JavaScript objects. Example:

// array is a object, you can add properties to it
var aa = [3,4];
aa["p1"] = 7;
console.log(aa);                // [ 3, 4, p1: 7 ]
// function is object, you can add properties to it
var ff = function () {return 4;};
ff["p1"] = 1;
console.log(ff);                // { [Function] p1: 1 }
console.log(ff["p1"]);          // 1
// date is a object, you can add properties to it
var dd = new Date();
dd["p1"] = 7;
console.log(dd); // { Mon, 03 Mar 2014 09:01:47 GMT p1: 7 }
console.log(dd["p1"]);          // 7
// regex is a object, you can add properties to it
var rx = /\d+/;
console.log(rx.test("123"));    // true
rx["p1"] = 7;
console.log(rx);                // { /\d+/ p1: 7 }
console.log(rx["p1"]);          // 7

Note: You cannot add properties to objects that have extensible attribute set to false. 〔☛ JavaScript: Object Extensible and Prevent Modification

Object Attributes: Prototype, Extensible, Class

Object has 3 associated info called attributes:

These “attributes” are not object properties, and there's no systematic way to read or set them. Some have special ways for you to read or write to, not all.

See:

blog comments powered by Disqus