Is it possible to achieve dynamic scoping in JavaScript without resorting to eval?

To add a note on this topic:

In JavaScript whenever you make use of:

  • function declaration statement or function definition expression then local variables will have Lexical Scoping.

  • Function constructor then local variables will refer to the global scope (top-level code)

  • this is the only built-in object in JavaScript that has a dynamic
    scoping and is set through the execution (or invocation) context.

So to answer to your question, In JS the this is already dynamically scoped feature of the language and you even don’t need to emulate another one.

Leave a Comment