Chapter 11. Scope: hiding information

This chapter covers

  • The dangers of global variables
  • The benefits of local variables
  • Using namespaces to reduce global variables
  • Creating local variables with functions
  • Returning an interface for the user

You want the user to be able to interact with your console-based programs. But you don’t want to give them too much control! This chapter explores ways of hiding parts of your program from the user at the console and clearly defining the properties and methods you expect them to use. For example, for a quiz program the user should be able to submit an answer but not change their score:

> quiz.submit("Ben Nevis")    // OK

> quiz.score = 1000000        // Don't let the user do this!

By separating the public ...

Get Get Programming with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.