Objects JS Flashcards

1
Q

In objects, what is a property?

A

A “key:value” pair, where key is a string (the name of the property) and the value van be anything

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

let user = new Object(); // “object constructor” syntax
let user = {}; // “object literal” syntax

What is happening here ?

A

Empty object is being created

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

Let’s create an object for an user, where we know the name and the age of the user.

A

let user = { // an object
name: “John”, // by key “name” store value “John”
age: 30 // by key “age” store value 30
};

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

How to access property values?

A

Property values are accessible using the dot notation:

alert( user.name ); // John

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

How to remove a property?

A

To remove a property, we can use the delete operator:

delete user.age;

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

What is a multiword property name?

A

We can also use multiword property names, but then they must be quoted:

“likes birds”: true

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

For multiword properties, the dot access doesn’t work. What do we use instead?

A

There’s an alternative “square bracket notation” that works with any string:

user[“likes birds”] = true;

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

What is a computed property?

A

We can use square brackets in an object literal, when creating an object. That’s called computed properties.

let fruit = prompt(“Which fruit to buy?”, “apple”);

let bag = {
[fruit]: 5, // the name of the property is taken from the variable fruit
};

alert( bag.apple ); // 5 if fruit=”apple”

The meaning of a computed property is simple: [fruit] means that the property name should be taken from fruit.

So, if a visitor enters “apple”, bag will become {apple: 5}.

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

The use-case of making a property from a variable is so common, that there’s a special property value shorthand to make it shorter. How would that be ?

A

Instead of name:name we can just write name, like this:

function makeUser(name, age) {
return {
name, // same as name: name
age, // same as age: age
// …
};
}

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

Can we use both normal properties and shorthands in the same object?

A

Yes!

let user = {
name, // same as name:name
age: 30
};

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

As we already know, a variable cannot have a name equal to one of the language-reserved words like “for”, “let”, “return” etc.

Does the same apply for object properties?

A

No! for an object property, there’s no such restriction, there are no limitations on property names.

Other types are automatically converted to strings.

For instance, a number 0 becomes a string “0” when used as a property key.

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

What is the Property existence test, “in” operator?

A

So we can easily test whether the property exists:

There’s also a special operator “in” for that.

The syntax is:

“key” in object

let user = { name: “John”, age: 30 };

alert( “age” in user ); // true, user.age exists

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

To walk over all keys of an object, we use?

A

The “for..in” loop

The syntax:

for (key in object) {
// executes the body for each key among object properties
}

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

If we loop over an object, do we get all properties in the same order they were added?

A

“ordered in a special fashion”: integer properties are sorted, others appear in creation order.

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

Integer properties? What’s that?

A

The “integer property” term here means a string that can be converted to-and-from an integer without a change.

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