пятница, 9 марта 2012 г.

Private data for objects in JavaScript

Private data for objects in JavaScript

JavaScript does not come with dedicated means for managing private data for an object. This post describes five techniques for working around that limitation:
  1. Instance of a constructor – private data in environment of constructor
  2. Singleton object – private data in environment of object-wrapping IIFE
  3. Any object – private data in properties with marked names
  4. Any object – private data in properties with reified names
  5. Single method – private data in environment of method-wrapping IIFE
The following sections explain each technique in more detail.

Required knowledge: While everything is explained relatively slowly, you should probably be familiar with environments and IIFEs [1] and with inheritance and constructors [2].

Комментариев нет:

Отправить комментарий