Loading

JS Fundamentals: Objects

Cory Brown

This is a live streamed presentation. You will automatically follow the presenter and see the slide they're currently on.

Object Instantiation

var o = new Object();

NO!

var o = new SpecializedObjConstructor();
var o = new Object();
var o = {};
var o = Object.create(proto, propDescriptors);

Object.create()

Object

.create(

    null,

    {

        property: {

            configurable: false,
            
            enumerable:   false,
            
            value:        undefined,

            writable:     false,

            get:          undefined,

            set:          undefined
        
        }

    }

);
`create` is a method on the `Object` constructor.

 

the first parameter is required and is an object to act as the newly created object's prototype. `null` is also a valid type here.

 

the second parameter is optional and is an object that defines how the new object's properties are configured.

configurable

If false (default:false), any attempts to delete the property or change the `configurable` descriptor will fail. `writable` and `enumerable` can still be changed.

enumerable

If false (default:false), the property will be NOT iterated over during a for..in (or similar) and will NOT be an item in the array returned by `Object.keys()`

writable

If false (default:false), the value of the property can not be changed.

value

the value of the property. If set (default:undefined), the `get` and `set` property descriptors must be undefined.

get

A getter function for the value. If set (default:undefined), the `value` property descriptor must be undefined.

set

A setter function for the value. If set (default:undefined), the `value` property descriptor must be undefined.

Datatype Emulation

Objects as Dictionaries

    uniqueKey : value
    .
    .
    .
{

  "twitter": "//twitter.com/uniqname",
    
  "linkedin": "//www.linkedin.com/profile/view?id=21174018",

  "github": "//github.com/uniqname"

}

that was easy!

Dictionary

Object

A dictionary (associative array, map, symbol table) is a data structure composed of a collection of key, value pairs.

Objects as Record

struct date {

   int year;

   int month;

   int day;

};
var date = {

    year: 1999,
    
    month: 12,

    day: 31

};

Record

Object

A record (struct, compound data) is a collection of elements, often fixed in number and sequence, indexed by names.

Objects as Sets

{

    "Cory",
    
    "Bruce",

    "Daniel"

}
{

    0: "Cory",
    
    1: "Bruce",

    2: "Daniel"

}

Set

Object

A data type that stores unordered and unduplicated values.

Objects as Arrays

[value, value, value, ...]
{

  0: "Cory",

  1: "Bruce",

  2: "Daniel",

  length: 3

}

Array

Object

A systematic arrangement of objects, usually in rows and columns