Meeting Objects and Functions Flashcards

1
Q

What are two non-primitive values in JS?

A

Objects and functions.

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

What are examples of objects?

A

Objects, arrays, dates, regexs, Math etc

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

What does it mean to be mutable?

A

The value can be changed! Either using dot or bracket notation.

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

Which of the 9 values in JS are mutable? Which are immutable?

A

Mutable: functions and objects

Immutable: numbers, strings, booleans, null, undefined, bigints, symbols

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

How are primitives and non-primitives different?

A
  • Primitives are not mutable, non-primitives are
  • Primitives cannot be created they can only be pointed to while non-primitives are mutable and can be created.
  • In our mental model, primitives have always existed (i.e. we merely point to them) but non-primitives don’t exist until we create them.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the following yield?

let x = {}
let y = {}
console.log(x === y)
console.log(x == y)

A
// false
// false

This is because every object is uniquely different. I.e. it might have the same internal values but it has a different referential equality.

Every object literal is unique. The same goes for every other type of object such as Date or Array.

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

In our mental model can we destroy objects?

A

Nope not really.

let junk = {};
junk = null; // Doesn't necessarily destroy an object

JavaScript doesn’t offer guarantees about when garbage collection happens.

It’s more like we don’t see them anymore. We can’t be sure the JS GC is gonna delete it - in fact we don’t really need to worry about the inner workings of GC except that it clears away unused debree floating around our planet and in times of crisis (i.e. too much debree around the planet and not enough memory we might need to dig in further to understand why so many are being created and clogging up our skies).

So we can create objects but we can’t really destroy them, just look away from them or unwire them.

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

In our mental model, are functions our code or values separate from our code like objects?

A

Yes and no. Functions are values in JS but they contain the code in which other values are used.

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

How many values do we pass the console.logs?

1.
for (let i = 0; i < 7; i++) {
console.log(2);
}

2. 
for (let i = 0; i < 7; i++) {
  console.log({});
}
3.
for (let i = 0; i < 7; i++) {
  console.log(function() {});
}
A
  1. Only one value as numbers are primitives.
  2. Seven different values as object literals are all unique.
  3. Seven different values as function expressions are all unique.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a good mental model for non-primitive literals?

A

We can think of non-primitive literals as expressions for new values rather than values in and of themselves.

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

Are function expressions unique? I.e.

let x = function() {}
let y = function() {}
x === y
x == y

A
// false
// false

Every time we execute a line of code that contains a function expression, a brand new function value appears in our universe.

Like any expression, a function expression is a “question” to our JavaScript universe—which answers us by creating a new function value every time we ask.

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

How are functions like objects?

A

They’re mutable, they can be created, each new expression creates a new value rather than pointing to some form of an object.

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

Are functions objects?

A

Technically, functions are objects in JavaScript.

In our mental model however we’ll keep treating them as a separate fundamental type because they have unique capabilities compared to regular objects.

But, generally speaking, if you can do something to an object, you can also do that to a function too. They are very special objects.

They are function objects colloquially called functions.

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

What are functions according to MDN?

A

Generally speaking, a function is a “subprogram” that can be called by code external (or internal in the case of recursion) to the function.

Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

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

What will the following yield:

let x = () => 2
typeof x
A

// ‘function’

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

In the following code what do we call the ‘countDwarves()’ part?

let countDwarves = function() { return 7; };
let dwarves = countDwarves(); // () is a function call
console.log(dwarves);
A

An expression - to be specific it is a call expression.