JS: Object (references) Comparison

When you create an object in Javascript, it stores the object at a unique location in memory.  So even when the objects look identical, they are not the same because JS is comparing to see if they are at the same memory address, not if they look the same or not.

Thus, {} === {}  would return false.

Notes: 

  • Values and variables of same type take same amount of fixed storage (allocated memory).
  • All objects, however, are not of the same size. When a new object is created, a composite memory block consisting of a series of consecutive blocks, one for each instance variable of the object, is created.  The sizes of two objects of different types can be different depending on the number and the types of their instances variables.
  • Since an object variable stores addresses, it also called a pointer variable or reference variable, and the address stored in it a pointer or reference.
JS: Object (references) Comparison

Primitive (values) vs Objects (references)

A variable can hold one of two value types: primitive values or reference values.

  • Primitive values are data that are stored on the stack.
  • Primitive value is stored directly in the location that the variable accesses.
  • Reference values are objects that are stored in the heap. (aka it’s stored elsewhere to preserve memory space and the variable is just a link to it).
  • Reference value stored in the variable location is a pointer to a location in memory where the object is stored.
  • 5 Primitive types: Undefined, Null, Boolean, Number, or String.  Anything else is an object.

The Basics:

Objects are aggregations of properties. A property can reference an object or a primitive.

Primitives are values, they have no properties.

Some Examples:

screen-shot-2017-02-22-at-9-37-22-pm

Primitive (values) vs Objects (references)

JavaScript Objects

Object.keys(obj) returns an array of keys in a hash object.

Examples:

var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']

// array like object with random key ordering
var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(an_obj)); // console: ['2', '7', '100']

Object.values(obj) returns an array of values in a hash object.

Examples:

var obj = { foo: 'bar', baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.values(obj)); // ['a', 'b', 'c']

// array like object with random key ordering
var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.values(an_obj)); // ['b', 'c', 'a']

obj.hasOwnProperty(prop) returns true/false indicating if an object has the specified property (key, NOT value).

Examples:

var object = {cat: 'meow'};
object.hasOwnProperty('cat'); // true
object.hasOwnProperty('meow'); // false

To access a property’s value:  object[key]

Example:  object['cat']; // 'meow'

JavaScript Objects