Prototypes Flashcards

1
Q

How is the following possible?

let pizza = {};
console.log(pizza.taste); // "pineapple"
A

Prototypes!

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

How do you add a prototype to an object?

A

You can use __proto__ like so:

let human = {
  teeth: 32
}

let gwen = {
__proto__: human,
age: 19
}

console.log(gwen.teeth) // 32

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

What is a good mental model for the __proto__ property?

A

Think of it as a special wire which creates a chain of objects of X length.

By specifying __proto__ (also known as our object’s prototype), we instruct JavaScript to continue looking for missing properties on that object instead.

You ask the object for something and it goes ‘I don’t know but x might know!’

A prototype is like a relationship.

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

What is a good mental model for expressions?

A

They’re questions to our JavaScript universe.

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

When do we get undefined for an object property?

A

We would only get undefined if we ran out of prototypes and still hadn’t found our property.

This is similar to saying, “I don’t know, but Alice might know.” Then Alice might say, “Actually, I don’t know either—ask Bob.” Eventually, you will either arrive at the answer or run out of people to ask!

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

What do we call a series of objects linked by prototype?

A

This sequence of objects is known as our object’s prototype chain.

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

Can prototype chains be circular?

A

Unlike a chain you might wear, prototype chains can’t be circular!

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

What would the following give you?

let human = {
  teeth: 32
}
let gwen = {
  \_\_proto\_\_: human,
  // This object has its own teeth property:
  teeth: 31
}

console. log(human.teeth)
console. log(gwen.teeth)

and why?

A
// 32
// 31

If gwen didn’t have its own teeth property, we would look at the prototype. But because the object that gwen points to has its own teeth property, we don’t need to keep searching for the answer.

In other words, once we find our property, we stop the search.

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

How do you check for a property while avoiding looking up the prototype chain?

A

You can call a built-in function called hasOwnProperty.

Example: human.hasOwnProperty(‘teeth’)

It returns true for “own” properties, and does not look at the prototypes.

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

What does the following give you?

let human = {
  teeth: 32
}
let gwen = {
  \_\_proto\_\_: human,
  // Note: no own teeth property
}

gwen.teeth = 31

console. log(human.teeth); // ?
console. log(gwen.teeth); // ?

A
// 32
// 31

gwen.teeth = 31 creates a new own property called teeth on the object that gwen points to. It doesn’t have any effect on the prototype.

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

What’s a simple rule of thumb for assignment relative to prototypes?

A

When we read a property that doesn’t exist on our object, we’ll keep looking for it on the prototype chain. If we don’t find it, we get undefined.

But when we write a property that doesn’t exist on our object, that creates the property on our object. Generally speaking, prototypes will not play a role.

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

Do brand new objects have null __proto__? I.e.

let obj = {}
console.log(obj.\_\_proto\_\_)
A

Nope! The prototype for any plain new object is The Object Prototype.

This includes all standard object properties and methods such as hasOwnProperty and toString.

All this time, we were thinking {} created an “empty” object, but it’s not so empty after all! It has a hidden __proto__ wire that points to the Object Prototype by default. This explains why JavaScript objects seem to have “built-in” properties.

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

What is the prototype of The Object Prototype?

A

// null

It is an object which truly doesn’t have a prototype at all.

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

Can you create an object with no prototype?

A

Yes:

let weirdo = {
  \_\_proto\_\_: null
}

but y u do that?

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

Why is adding things to, say, the object prototype generally considered bad practice?

A
  1. fragile (if many libraries did this, they might override each other)
  2. makes it hard to add new features - i.e. if jquery had a .reduce function adding an ES6 implementation which has a different API might cause a ruckus
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s the difference between the __proto__ and prototype properties?

A

The story around this is confusing. Before JavaScript added classes, it was common to write them as functions that produce objects.

Adding a .prototype onto one such ‘class’ function would set all new objects to have that designated ‘__proto__’:

function Donut() {
  return { shape: 'round' }
}
Donut.prototype = {
  eat() {
    console.log('Nom nom nom')
  }
}
let donut1 = new Donut() // \_\_proto\_\_: Donut.prototype
let donut2 = new Donut() // \_\_proto\_\_: Donut.prototype

To conclude, a function’s prototype specifies the __proto__ of the objects created by calling that function with a new keyword.

17
Q

Is __proto__ an accepted approach for changing prototype?

A

Nope! It’s actually deprecated due to some shift as complexities in JS language definition.

18
Q

What should we use instead of proto if we really wanted to manage prototypes?

A
  1. Use Object.getPrototypeOf() to get the prototype of an object.
  2. Use Object.create() to create a new object with a given prototype. Avoid Object.setPrototypeOf(). If you change the prototype of an existing object, it can become slower.
  3. You can use __proto__ as an operator in an object literal. It is useful for demonstrating prototypal inheritance and for creating dict objects. However, there are complexities and caveats: https://2ality.com/2015/09/proto-es6.html
19
Q

What are prototypes typically used for in modern JS?

A
  • people often use prototypes to create a traditional “class inheritance” model that’s popular in other programming languages. This became so common that JavaScript added a class syntax as a convention that “hides” prototypes out of sight.
20
Q

What’s a simple example of looking up the prototype chain?

A

When reading obj.something, if obj doesn’t have a something property, JavaScript will look for obj.__proto__.something. Then it will look for obj.__proto__.__proto__.something, and so on, until it either finds our property or reaches the end of the prototype chain.