Real-world programs are nasty beasts. You have seen by now that in JavaScript (and indeed, any programming language) you have to account for every instruction and every piece of data in your program. While this is not that difficult if you are doing something stupid like displaying the contents of a string in the colours of the rainbow, it can become extremely taxing in larger programs. Look at the menu bar on any program and pick one of the items. Think about what data that control needs, and what instructions it carries out. And then think about coordinating and managing all of the controls in the entire program! As you can see, it could quickly become something of a nightmare.
The idea behind abstraction is to hide complexity from yourself and other programmers. Recall from the article on the top-down approach to programming that an easy way to approach programming challenges is to divide the problem into its components, and then implement them in the reverse order. Abstraction adds the notion of hiding the details of components as you build the program.
Do you remember how the algorithm for creating and retrieving cookies works? More to the point, should you? I've forgotten the details of its implementation simply because I never see them. In fact, I intentionally go out of my way to hide them from myself! As far as I'm concerned, the only thing that's required to create cookies is the following code:
<script language="JavaScript" src="cookies.js"> setCookie("myCookie", "cookieValue"); myValue = getCookie("myCookie"); </script> |
Contrast that with the original article, which took two pages and examples on the CD to explain!
Note: Hard-core C programmers will notice that I'm going to fudge a little around the edges when I speak about abstraction in relation to JavaScript. By their very nature, JavaScripts are not as complicated as C programs, and C-style abstraction, even if it were possible, would not provide great rewards in JavaScript. So, if my loose interpretation of abstraction concepts annoys you, you'll just have to grit your teeth.