Skip to content

workshop - chain of fallbacks #1

Description

@serapath

Prototype Chain (= "chain of fallbacks") - How does it work in it's core?

  1. "fallbacks" are a main pillar of JavaScript together with "callbacks" which is another one.
  2. A lot of JavaScript syntax uses this core mechanism, for example: (new, .prototype, class, ...)
  3. 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)
  4. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions