let computer = {
type: 'macBookPro',
storage: '1TB',
memory: '16GB',
yearReleased: '2020',
}
testObject['color'] = 'green'
key | value |
---|---|
color
'green'
testObject.number= 10
number
10
const idName = 'id'
id
testObject[idName] = '2525'
'2525'
- bracket notation
- dot notation
- bracket notation
key | value |
---|---|
color
'green'
number
10
id
'2525'
testObject
let val1 = testObject['color']
val1 = 'green'
let val2 = testObject.number
bracket notation
dot notation
val2 = 10
Set method inside the Object Literal
let testObject = {
sayHello: function(){
console.log('Hello testObject')
}
}
testObject.sayHello()
'Hello testObject'
let testObject = {
sayHello: function(){
console.log('Hello testObject')
}
}
testObject.sayGoodbye = function(){
console.log('Goodbye testObject')
}
OR
testObject['sayGoodbye'] = function(){
console.log('Goodbye testObject')
}
testObject.sayGoodbye() Invocation
'Goodbye testObject'
a. objects are collections that store properties as unique keys & value pairs
b. we can get and set properties by using either bracket notation or dot notation
c. methods are just functions encapsulated inside an object.