Objects Flashcards
Objects - 7th type of data in JS. An object is a built-in data type for storing key-value pairs. Data inside objects are unordered, and the values can be of any type.
What is JS Objects?
There are 7 data types in JS. 6 of those are primitive data types:
- string
- number
- boolean
- null
- undefined
- symbol
7th- Objects-> we can you Objects to model real- world things like a basketball.
JS Objects are containers storing related data and functionality.
Creating OBJECT literals?
Objects can be assigned to variables just like any other JS data types
We use curly braces { } to designate an object literal.
let spaceship = { } ; // spaceship is an empty object
We fill an object with unordered data.this data is organised into key-value pairs.
A key is like a variable name that holds a value.
object
let spaceship = { 'Fuel Types' : 'diesel' , color: 'silver' }; key-value properties
The spaceship object has 2 properties: Fuel type and color. Fuel type has quotation mark because it contains a space character.
Accessing Object properties with dot notation
1.with dot notation:
‘hello’.length // returns 5
let spaceship ={ homePlanet: 'Earth', color: 'silver' };
spaceship.homePlanet; // Earth
spaceship.color; //silver
| |
objects property name
Trying to access a property that doesn’t exist on that Object
Undefined will be returned
spaceship.favoriteIceCream; // undefined
Second way to access a key’s value in an Object
Bracket notation
To use Bracket Notation [ ] to access Objects property, we pass in the property name (key) as a string:
let spaceship = { 'Fuel Type' : 'Turbo Fuel' , 'Active Duty' : true, homePlanet : 'Earth', numCrew: 5 };
spaceship [‘Active Duty’]; //true
Using variable inside the bracket to select the keys of an Object
let returnAnyProp = ( objectName, propName) => objectName [propName];
returnAnyProp (spaceship, ‘HomePlanet’); // Earth
Property assignment of Objects
Objects are mutable - we can update them after we create them.
We can use dot notation or bracket notation [ ] and the assignment operator = to add new key- value pairs to an object or change existing property.
spaceship [’ Fuel Type’] = ‘vegetable oil’,
spaceship.color = ‘gold’;
- if the property already exists on the Object, whatever value it held before will be replaced with the newly assigned value.
- if there was no property with that name, a new property will be added to the Object.
Deleting a property from an Object
using delete operator
const spaceship = { ' Fuel Type' : ' Turbo Fuel', homePlanet : 'Earth', mission: 'Explore the universe' };
delete spaceship.mission; // removes the mission property
Methods in Objects
When data stored on an Object is a function we call that a method
A property is what an Object has.
METHOD is what an Object does!!!
Ex: console is a global JS Object
.log is a method on that Object
Math is a global JS Object.
.floor is a method on it.
Object methods are invoked by appending the Objects name with the dot operator followed by the method name and parenthesis:
const alienship = { invade ( ) { console.log ("Hello....) } };
alienship.invade( ); // returns “ Hello ….
Nested Objects
An Object might have another Object as a property which in turn might have a property that’s an array of even more Objects.
const spaceship = { telescope: { year built: 2018, model: '910I-XLT', focallength: 2032 }, crew: { captain: { name: 'Sandra', degree: 'Computer Engineering', encourage team ( ) { console.log('We got this!') } } } };
Pass by Reference
Objects are passed by reference. This means when we want to pass a variable assigned to an Object into a function as an argument, the computer interprets the parameter name as pointing to the space in memory holding that Object.As a result, functions which change Objects properties actually mutate the object permanently (even when the Object is assigned to a const variable).
Looping through Objects
Loops are programming tools that repeat a block of code until a condition is met.
we learned how to iterate through arrays using their numerical index, but key-value pairs in Objects aren’t ordered.
JS alternative solution for iterating through Objects with for….in syntax
for ( let variableName in outerObject.innerObject) {
console.log (‘${OuterObject.innerObject[variableName].propertyName}: ${OuterObject.innerObject[variableName].differenPropertyName}’)
};
Key-valur pairs
Key-value pairs is a property -when property is a function it is known as a method.
Key-valur pairs
Key-value pairs is a property -when property is a function it is known as a method.
An Object literal
An Object literal is composed of comma-separated key-value pairs surrounded by curly braces.