Prototypes Flashcards
How is the following possible?
let pizza = {}; console.log(pizza.taste); // "pineapple"
Prototypes!
How do you add a prototype to an object?
You can use __proto__ like so:
let human = { teeth: 32 }
let gwen = {
__proto__: human,
age: 19
}
console.log(gwen.teeth) // 32
What is a good mental model for the __proto__ property?
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.
What is a good mental model for expressions?
They’re questions to our JavaScript universe.
When do we get undefined for an object property?
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!
What do we call a series of objects linked by prototype?
This sequence of objects is known as our object’s prototype chain.
Can prototype chains be circular?
Unlike a chain you might wear, prototype chains can’t be circular!
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?
// 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 do you check for a property while avoiding looking up the prototype chain?
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.
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); // ?
// 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.
What’s a simple rule of thumb for assignment relative to prototypes?
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.
Do brand new objects have null
__proto__? I.e.
let obj = {} console.log(obj.\_\_proto\_\_)
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.
What is the prototype of The Object Prototype?
// null
It is an object which truly doesn’t have a prototype at all.
Can you create an object with no prototype?
Yes:
let weirdo = { \_\_proto\_\_: null }
but y u do that?
Why is adding things to, say, the object prototype generally considered bad practice?
- fragile (if many libraries did this, they might override each other)
- 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