Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workshop - chain of fallbacks #1

Open
Tracked by #5
serapath opened this issue May 5, 2020 · 0 comments
Open
Tracked by #5

workshop - chain of fallbacks #1

serapath opened this issue May 5, 2020 · 0 comments

Comments

@serapath
Copy link
Member

serapath commented May 5, 2020

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.

@serapath serapath mentioned this issue May 5, 2020
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant