-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Prototype Chain (= "chain of fallbacks") - How does it work in it's core?
- "fallbacks" are a main pillar of JavaScript together with "callbacks" which is another one.
- A lot of JavaScript syntax uses this core mechanism, for example: (
new,.prototype,class, ...) - What is it all about?
- If you look for a thing in a place and cant' find it
- then go to the next place where you might find it and if you can't find it there
- then go to the next place where you might find it and if you can't find it there
- then go to the next place where you might find it and if you can't find it there
- then go to the next place ...unless it's the end of the list of "fallback places" (that are used to look for stuff in case you couldnt find it)
- How does it look in code?
var place = {}
var fallback_place1 = { A: 1, B: 2, C: 3, foo: x => 'foo', D: 4 }
var fallback_place2 = { E: 1, F: 2, G: 3, bar: x => 'bar', H: 4 }
var fallback_place3 = { I: 1, J: 2, K: 3, baz: x => 'baz', L: 4 }
var fallback_place4 = { M: 1, N: 2, O: 3, quux: x => 'quux', P: 4 }
// Set of the prototype chain (= "chain of fallbacks")
place.__proto__ = fallback_place1
fallback_place1.__proto__ = fallback_place2
fallback_place2.__proto__ = fallback_place3
fallback_place3.__proto__ = fallback_place4
// look for "quux"
console.log(place.quux()) // => 'quux'
Now when you learn about JavaScript Syntax like (class, .prototype, new, extends, Object.create, Object.setPrototypeOf`, ...), it essentially always comes back to the above.
Metadata
Metadata
Assignees
Labels
No labels