Hacker News new | ask | show | jobs
by marrs 1834 days ago
JS has dynamic scoping via call, apply, and bind.
1 comments

So you do e.g.

  { option: true, __proto__: bar }.foo(…)
To extend

  bar.foo(…)
?

(I think you are implying that functions would care about properties of this and that one would achieve “dynamic scoping” by modifying what this is.)

The first issue is that this doesn’t go deep in the stack. E.g. if you want to affect the option used by the foo function on a whole list of objects, it is hard. If you want to affect a call deeper in the stack that can be very hard (elisp example: some extension might want to switch to a buffer but you might want it to pop up the buffer or make some other windowing choice. The solution is that by dynamically binding certain variables around where this extension/function is called, you can make switch-to-buffer call display-buffer and you can override the way display-buffer behaves)

The second issue is that it is ugly.

Compare the elisp example of:

  (let ((option t))
    (bar-foo bar))
bar-foo can call baz which can call whizz which could then depend on the value of option.