Chapter 9. Ninja alchemy: runtime code evaluation
This chapter covers
- How runtime code evaluation works
- Different techniques for evaluating code
- Using evaluation in applications
- Decompiling functions
- Namespacing
- Compressing and obfuscating
One of the many powerful abilities that distinguish JavaScript from many other languages is its ability to dynamically interpret and execute pieces of code at runtime. Code evaluation is simultaneously a powerful, as well as a frequently misused, feature of JavaScript. Understanding the situations in which it can and should be used, along with the best techniques for using it, can give us a marked advantage when creating advanced application code.
In this chapter, we’ll explore the various ways of interpreting code at runtime and the situations in which this powerful ability can lift our code into the big leagues. We’ll learn about the various mechanisms that JavaScript provides to cause code to be evaluated at runtime, and we’ll see how runtime evaluation can be applied to various interesting scenarios that we’re likely to run into when creating web applications.
To start, let’s find out just how we can cause code to be evaluated at runtime.
Within JavaScript there are a number of different mechanisms for evaluating code. Each has its own advantages and disadvantages, and which one we use should be chosen carefully based upon the context in which it’s being employed.
These various means include