{"library":"private","title":"Private State Utility","description":"The `private` package (version 0.1.8) provides a utility for associating truly private state with any JavaScript object, predating native private class fields. It achieves this primarily through two mechanisms: `makeAccessor`, which uses closures to create a secret object accessible only via a dedicated accessor function, and `makeUniqueKey`, which generates non-enumerable, unguessable property names. A crucial update in v0.1.2 addressed memory leak issues by ensuring secret objects are directly (but securely) stored on owning objects, allowing them to be garbage collected when the owner becomes unreachable. This package, last updated significantly years ago, is largely superseded by modern JavaScript features like private class fields (`#field`) but historically offered a robust solution for encapsulation in ES5 environments.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install private"],"cli":null},"imports":["const getSecret = require('private').makeAccessor();","const secretKey = require('private').makeUniqueKey();","const Private = require('private');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const getSecret = require(\"private\").makeAccessor();\n\nconst user = {\n  id: 'user-123',\n  name: 'Alice',\n  email: 'alice@example.com'\n};\n\n// Associate private data with the 'user' object\ngetSecret(user).passwordHash = \"$2a$10$abcdefghijklmnopqrstuv.w.x.y.z\";\ngetSecret(user).apiTokens = ['token-a', 'token-b'];\n\n// Public properties are visible\nconsole.log('Public user keys:', Object.keys(user)); // [ 'id', 'name', 'email' ]\nconsole.log('Public user properties:', Object.getOwnPropertyNames(user)); // [ 'id', 'name', 'email' ]\n\n// Private data is only accessible via the accessor function\nconst userData = getSecret(user);\nconsole.log('User private data:', userData); // { passwordHash: '$2a$10$abcdefghijklmnopqrstuv.w.x.y.z', apiTokens: [ 'token-a', 'token-b' ] }\nconsole.log('User password hash:', userData.passwordHash);\n","lang":"javascript","description":"Demonstrates how to associate truly private state with a JavaScript object using `makeAccessor`, preventing direct enumeration or access.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}