A post on objects and their methods in javascript
Post created on: 2/5/2021
Objects are a key part of JavaScript. In their most simple form, they are key-value pairs made up of properties/keys and values e.g.
let myTeam = {
name: 'Liverpool',
stadium: 'Anfield',
city: 'Liverpool'
}
// In this object literal example the properties/keys are name, stadium and city with Liverpool, Anfield and Liverpool being the values
As with arrays properties can be added to an object using either dot (.
) or bracket []
notation. Note if the property name has a space in it or you want to use a variable you will need to use bracket notation e.g.
myTeam.manager = 'Jurgen Klopp'
myTeam[founded] = 1892
myTeam['trophies won'] = { 'European Cups': 6, 'League Titles' : 19}
// myTeam now contains the following which shows as with arrays you can nest objects inside objects e.g.
myTeam = {
name: 'Liverpool',
stadium: 'Anfield',
city: 'Liverpool',
manager: 'Jurgen Klopp',
founded: 1892,
'trophies won': {
'European Cups' : 6,
'League Titles': 19
}
myTeam['trophies won']['European Super Cups'] = 3
// ...
'trophies won' : {
'European Cups' : 6,
'League Titles' : 19,
'European Super Cups' : 3
}
Access properties follow the same format e.g.
myTeam.manager // 'Jurgen Klopp'
myTeam['trophies won'] // { 'European Cups': 6, 'League Titles' : 19, 'European Super Cups' : 3 }
myTeam['trophies won']['European Super Cups'] // 3
To check if an object has a specific property you can use
let myTeam {
name: 'Liverpool',
stadium: 'Anfield',
city: 'Liverpool',
manager: 'Jurgen Klopp'
}
myTeam.hasOwnProperty('manager') // returns true as this prop exists in the object
myTeam.hasOwnProperty('founded') // returns false as this prop does not exist in the object
for(let val in myTeam){
console.log(val)
}
// returns name, stadium, city, manager, trophies won
for... in
as it will provide the values in the same order, but it will return an array e.g. Object.values(myTeam)
// returns ['Liverpool', 'Anfield' 'Liverpool', 'Jurgen Klopp']
let teams = {
Liverpool: {
founded: 1892,
manager: 'Jurgen Klopp'
},
Everton: {
founded: 1888,
manager: 'Carlo Ancelotti'
},
Southampton: {
founded: 1885,
manager: 'Ralph Hasenhuttl'
}
}
Object.keys(teams) // ['Liverpool', 'Everton', 'Southampton']
To remove properties from an object, use the delete
keyword e.g.
let myTeam = {
name: 'Liverpool',
stadium: 'Anfield',
city: 'Liverpool',
}
delete myTeam.city // myTeam: { name: 'Liverpool', stadium: 'Anfield' }
Object.freeze(myTeam)
myTeam.fullName = 'Liverpool Football Club'
// this will be ignored as the freeze prevents any mutation to the myTeam object