Objects Flashcards

1
Q

Object

A

• data structure that stores data in key-value pairs.

var person = {
    name: "Nigel",
    age: 21,
    city: "La"
};
  • keys are always strings
  • values can be any type or variable
  • objects are similar to arrays, but objects use strings, it’s “key”, to access the different elements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

key-value pair

A

• A pair made up of a string(called a key) that is matched with a particular value (which can be any type of Value). Key-value pairs go inside JavaScript objects, and they are used to define an objects properties and methods

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

What is a Property?

A
  • A name for a key value pair in an object

* properties usually tell you something about an object.

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

Methods

A
  • A method is a function associated with an object.
  • Methods perform actions on the object

add: function(x, y) {
return x + y;
}

obj.add(10,5) //15

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

How do you retrieve Data out of an object?

A
  • bracket notation, similar to arrays:
  • objects are similar to arrays, but objects use strings, it’s “key”, to access the different elements.

console.log(person[“name”]);

• dot notation:
console.log(person.name);
• dot notation only works if the key doesn’t have any special characters like spaces, etc

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

What’s the difference between bracket vs doted notations?

A

1) you can not use dot notation if the property starts with a number.
someObject.1blah //invalid
someObject[“1blah”] //valid
2) can’t use dot notation for property names with spaces
someObject.fav color //invalid
someObject[“fav color”]; //valid

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

Updating Data

A
//to update age
person["age"] += 1;
//to update city
person.city = "London";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you create an object?

A
1) make an empty object then add to it. 
var person = {}
person.name = "Nigel"
person.age = 21;
person.city = "LA";
2) All at once
var person = {
  name = "Nigel",
  age = 21,
  city = "La"
};
3) use "new object();" keyword
var person = new object();
person.name = "Nigel";
person.age = 21;
person.city = "La";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a library?

A

• A collection of JavaScript code that someone else wrote that we can use in our own programs.

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

Object.keys(anyObject);

A

• returns an array containing all the keys of anyObject.

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