Section 4: Objects and Functions Flashcards

1
Q

What are objects in javascript?

A

Collections of name/value pairs sitting in memory. They can contain numbers, methods (functions), etc.

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

What can objects contain?

A
  1. Primitive “property” 2. Object “property” 3. Function “method” (still a function but connected to an object so it’s called a method. An object sits in memory and has other things sitting in memory connected to it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the new object syntax?

A

var person = new Object(); (note: This is not the best way to create an object)

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

Computed member access operator

A

objectName[] (operator is the brackets. Inside brackets is a property or method name. Inside brackets you insert the value of the name you’re trying to access in memory.)

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

What does this log to the console? var person = new Object(); person[“firstname”] = “Tony”; var firstNameProperty = “firstname”; console.log(person); console.log(person[firstNameProperty]);

A

Tony

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

What is the easier/clearer was to access operators than the CMAO? (computed member access operator)

A

. operator (spoken “dot operator”)

e.g. console.log(person.firstname);

>> the dot operator takes two parameters, object before the dot and the name of the value you’re looking for after the dot.

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

What is the dot operator also called?

A

Member access operator

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

How do you use a dot operator to add properties to a sub/child object?

A

Example (note: left associativity!):

person. address = new Object();
person. address.street = “111 Main St.”;
person. address.city = “New York”;
person. address.state = “NY”;

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

What does the following look like with dot operators in place of computer member access operators?

console.log(person[“address”][“state”]);

A

console.log(person.address.state);

These offer the same results yet the preferred method is using the dot operator unless you really need to access properties using some kind of dynamic string.

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

What is an object literal?

A

var person = {};

The above is a shorthand and the same as new Object(); When the browser comes across a curly brace it assumes you’re creating an object.

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

Insert properties firstname and lastname into object person using object literal notation.

A

var person = { firstname: ‘Tony’, lastname: ‘Alicea’ };

Also, as a more readable version:

var person = {

firstname: ‘Tony’,
lastname: ‘Alicea’,
address: {
street: ‘111 Main St.’,
city: ‘New York’,
state: ‘NY’

}

};

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

Create an object on the fly with function greet and names firstname and lastname.

A

greet({

firstname: ‘Mary’,
lastname: ‘Doe’

});

note: you can create an object wherever you want and you can use it whenever you use any variable.

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

Define namespace

A

A container for variables and functions

Typically to keep variables and functions with the same name separate

NOTE: Javascript doesn’t have namespaces. Because of the nature of objects, we don’t need namespaces as a feature.

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

Primitive values don’t have any ___ or ___

A

properties or methods

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