Objects Flashcards

1
Q

How can can create an object and once you’ve made one, how could you edit one value of a key?

A
let person = {
name: 'Scott',
age: 18,
amVirgin: true

};
person.name = ‘Scottttyyyy’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you access a key’s value from an object? (2 Ways)

A

console.log(person.name);

or

console.log(person[name]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is one advantage that bracket notation has over dot?

A

Using bracket notation you can access variables inside the brackets to select a key from an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Within an object you can assign keys to strings, numbers, booleans and arrays, but how can you assign a key to a function? (Give example)

A

sayHello = () {
return Hello there, ${this.name};

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we create a method/faction to operate on data inside of the same object? (Give Example)

A

You use the this keyword, like so:

amVirgin = () {
if (!amVirgin) {
  return 'Gotta work on that!';
}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

You can use the this keyword in a method/function to access data within the object. What if we wanted to access the name not in my object but a different one, to log a different name to the console?

A
let myself = {
name: 'Scott',
sayHello() {
return `${this.name} says hello!`;
}

};

let friend = {
name: 'Mohit',
};
friend.sayHello = person.sayHello();
console.log(friend.sayHello())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What method can you use together within an object to update the value of a key according if new data is valid?

A

You an use setter methods, like so:

let person = {
_age: 18,
set age(newAge) {
if (typeof newAge === 'number') {
this._age = newAge;
console.log(`${newAge} is a valid input, age updated to ${newAge}`);
} else {
   console.log(`${newAge} is not a valid input, please make sure to enter a number as input`);
}
}
};
person.age = 19;
console.log(person._age);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly