Objects in JavaScript are containers that store data and functionality - an object is a versatile data type of its own
An object is created by using the {}
notation to designate the object literal
Syntax
let objectName = {};
- above,
objectName
is an empty object
An object is filled with unordered data, which are organized into key:value
pairs separated by ,
> ex)
let pomskyUni = { mom: 'husky', dad: 'pomeranian', gender: 'female' };
Each of the object's key:value
pair is a property of the object
key
is the property name (is a string data type)value
is the property valueFor property names with no special characters, whitespaces, or numbers, the ''
notation can be omitted. Otherwise, the property name must be enclosed in ''
> ex)
let pomskyUni = { mom: 'husky', dad: 'pomeranian', gender: 'female' 'favorite snack': 'duck meat', 'birth year': 2014 };
An object's properties can be accessed in two ways
The
.
notationlet getGender = pomskyUni.gender; console.log(getGender); // Prints: female
The
[]
notationlet getGender2 = pomskyUni['gender']; console.log(getGender2); // Prints: female
- note that, when using the
[]
notation, the property name is passed in as a string with the''
notation- When accessing keys(property names) with special characters, whitespaces, or numbers, you must use the
[]
notation- You can also input a variable as the key of the object using the
[]
notation:let girlOrBoy = 'gender'; console.log(pomskyUni[girlOrBoy]); // Prints: female
Objects are mutable - You can assign additional properties to an existing object
Two ways to assign properties to an object:
.
notation[]
notationThere are two results that can happen with property assignment:
In addition to properties, an object can have methods - methods are functions stored on an object
Just like an object's property, its methods are organized into key:value
pairs
key
is the method namevalue
is an anonymous function expressionSyntax
key:value
function expression:let objectName = { methodName: function() { code block; };
- new ES6 syntax:
let objectName = { methodName () { code block; };
> in the new ES6 syntax, we can omit the
:
and thefunction
keyword
Object's property is what it has, method is what it does
Objects themselves can be properties or methods of another object
> ex)
let pomskyUni = { gender: 'female', 'birth year': 2014, favorites: { 'daytime snack': 'duck recipe', hobby: 'walking', person: 'me' } };
Nested objects are accessed by chaining the .
and/or []
operators
>ex)
console.log(pomskyUni.favorites['daytime snack']); //Prints: duck recipe
When an object is passed in as an argument into a function, it is passed by reference - the function takes the object and refers to the memory location where the object is stored
> ex)
let object1 = { 'test no': 1, 'is changed': false }; let pbrTest = obj => { obj['is changed'] = true; }; pbrTest(object1); console.log(object1); // Prints: {'test no': 1, 'is changed': true}
> ex)
let object2 = { 'test no': 2, 'is changed': false } let pbrTest2 = obj => { obj = { 'test no': 3, 'is changed': true } console.log(obj); } pbrTest2(object2); // Prints: {'test no': 3, 'is changed': true} console.log(object2); // Prints: {'test no': 2, 'is changed': false}
You can iterate through the elements of an array, which are ordered data, by using the array index
For objects, you can iterate through objects using a special syntax - the for... in
syntax
> ex)
let objectName = { 'key1': { 'innerObject1': { 'inner2Property1': 'one', 'inner2Property2': 'two' }, 'innerObject2': { 'inner2Property1': 'three', 'inner2Property2': 'four', 'inner2Property3': 'five' } }, 'key2': { 'innerObject1': 'six', 'innerObject2': 'seven' } }; for (let innerKey in objectName['key1']) { console.log(`${innerKey}'s property 1: ${objectName['key1'][innerKey]['inner2Property1']}`); } /* Prints: innerObject1's property 1: one innerObject2's property 1: three */