liveBook · Manning

Private member

As explained in Functional Programming in JavaScript

A private variable is a program value that is intentionally inaccessible from outside its defining abstraction.

A private variable is a value kept inside an abstraction rather than exposed directly. In JavaScript, a closure lets returned functions continue accessing variables from the defining function, so an object can provide controlled access without exposing the variables themselves. section 2.4

A closure-backed zip code

The zipCode function stores code and location in local variables, then returns accessor functions. After zipCode finishes, princetonZip.code() still reads the retained code, while callers cannot directly access the local variable. section 2.4

The pattern depends on lexical scope: a function retains access to the environment surrounding its declaration, even after the enclosing function has returned. section 2.4

How Functional Programming in JavaScript covers it

How deep it goes
Explains lexical scope and retained environments through a closure-backed object example.
Examples
Demonstrates a zipCode factory whose returned accessors retrieve retained code and location values.
What you can do after
Apply it via JavaScript closures to retain internal state and reveal selected operations through returned functions.
How it connects
Connects private variables with closures, lexical scope, nested functions, and object methods.

Also explained in

Compare how 12 books explain private member →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.