Variables, Scope, and Memory Flashcards

1
Q

What are primitive values?

A

Simple atomic pieces of data

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

What are reference values?

A

Objects that may be made up of multiple values

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

What are the 5 primitive types?

A

Undefined, Null, Boolean, Number, String

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

What is an example of a reference type in JavaScript?

A

an Object. When you manipulate an object, you’re really working on a reference to that object rather than the actual object.

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

Can you add properties to primitive values?

A

No. Only to reference values.

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

What is the value of num2?

var num1 = 5;
var num2 = num1;
var num1 = 6;
A

5

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

When a reference value is assigned to a variable what is the actual value of the variable?

A

A pointer to an object stored on the heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
var obj1 = new Object();
var obj2 = obj1;
obj1.name = "Scott"

What is the value of obj2.name?

A

“Scott”

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

What does passed by value mean?

A

a value is copied from one variable to another.

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

What does passed by reference mean?

A

the location of the value in the memory is passed from one variable to another (Objects)

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

How are function arguments passed in ECMAScript?

A

They are passed by value. Function arguments should be considered nothing more than local variables within functions.

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

What is the instanceof operator used for and what does the syntax look like?

A

it is used to determine what type of object an object is.

result = variable instanceof constructor

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

What does this return?

person instanceof Object

A

true (if person is an object.)

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

typeof(obj) => true

If you want more information about this object what operator can you use?

A

obj instanceof Object => true

obj instanceof Array => false

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

What is the global execution context?

A

The outermost context. It changes depending on the host environment but in web browsers the global context is the window object.

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

What is a variable object?

A

Each execution context has an associated variable context upon which all of its defined variables and functions exist.

17
Q

What is the purpose of a scope chain?

A

to provide ordered access to all variables and functions that an execution context has access to.

18
Q

What is the front of the scope chain?

A

the variable object of the context whose code is executing. If the context is a function, then the activation object is used as the variable object.

19
Q

What does an activation object start with?

A

a single defined variable called arguments. (this doesn’t exist in global context)

20
Q

Do function arguments follow the same access rules as other variables in the execution context?

A

Yes, function arguments are considered to be variables.

21
Q

What are the two primary types of executions contexts?

A

global and function

22
Q

which two statements can cause a temporary addition to the front of the scope chain that is later removed after code execution?

A
  1. The catch block in a try-catch statement

2. A with statement

23
Q

Are there block-level scopes in JavaScript?

A

No.

24
Q

When a variable is declared using var, what context is it added to?

A

The most immediate context

25
Q

If a variable is initiated without first being declared what context does it get added to?

A

The global context

26
Q

Is it faster to access local variables or global variables?

A

Local variables because there’s no need to search up the scope chain.

27
Q

What is a garbage collection language?

A

The execution environment is responsible for managing the memory required during code execution.

28
Q

What is mark-and-sweep?

A

The most popular form of garbage collection. When a variable goes in context it’s flagged as in context. When a variable goes out of context it’s flagged as out of context.

When the garbage collector runs it marks all variables stored in memory, then it clears the marks off of variables that are in context and variables referenced by in context variables. The garbage collector then does a memory sweep destroying each of the marked values and reclaiming the memory.

29
Q

What is reference counting?

A

A type of garbage collection. The idea is that every value keeps track of how many references are made to it. When the count goes to zero its safe to reclaim the associated memory.

30
Q

What is a circular reference?

A

When object A has a pointer to object B and object B has a reference to object A.

31
Q

Why should you break the connection between native javascript objects and DOM elements?

myObject.element = null;
element.someObject = null;
A

To avoid circular reference problems.

32
Q

Where are primitive values stored?

A

They are a fixed size so they are stored in memory on the stack

33
Q

Where are reference values stored?

A

Reference values are objects and are stored in memory on the heap.

34
Q

What should you do with global variables to help with memory reclamation?

A

They should be dereferenced when they are no longer needed.